Home > Blockchain >  Error when trying to serialize std::wstring with boost::serialization
Error when trying to serialize std::wstring with boost::serialization

Time:11-19

I'm trying to serialize a class with a std::wstring variable, but what I'm getting are multiple undefined reference to ~ errors.

I don't seem to be missing any headers or libraries & from what I've read from the boost::serialization documents, std::wstring seems to be a primitive type that doesn't need any overriding.

I've included the following headers:

#include <boost/archive/text_woarchive.hpp>
#include <boost/archive/text_wiarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/string.hpp>
#include <boost/serialization/vector.hpp>
#include <iostream>
#include <sstream>
#include <string>
#include <locale>

The class I'm trying to serialize looks like this:

class A
{
public:
    A() = default;

    void setWstr(const std::wstring &wstr)
    {
        wstr_ = wstr;
    }

    const std::wstring &getWstr()
    {
        return wstr_;
    };

private:
    std::wstring wstr_;

    friend class boost::serialization::access;

    template <typename Archive>
    void serialize(Archive &ar, const unsigned int version)
    {
        ar &boost::serialization::make_nvp("wstr", wstr_);
    }
};

int main()
{
    std::wstring sdn = L"src dept";

    A test;
    test.setWstr(sdn);

    std::wstringstream ss;
    boost::archive::text_woarchive oa(ss);
    oa << test;

    return 0;
}

The errors I'm getting are these:

in function `text_woarchive_impl':
undefined reference to `boost::archive::basic_text_oprimitive<std::basic_ostream<wchar_t, std::char_traits<wchar_t> > >::basic_text_oprimitive(std::basic_ostream<wchar_t, std::char_traits<wchar_t> >&, bool)'

in function `text_woarchive_impl':
undefined reference to `boost::archive::basic_text_oarchive<boost::archive::text_woarchive>::init()'

`~text_woarchive_impl':
undefined reference to `boost::archive::basic_text_oprimitive<std::basic_ostream<wchar_t, std::char_traits<wchar_t> > >::~basic_text_oprimitive()'

Is there a way to fix this?

ETA) I've linked the following libraries

 -lboost_serialization  -lpthread  -lboost_system  -lboost_program_options  -lboost_chrono

CodePudding user response:

The serialization objects are split into two libraries: boost_serialization (which you are linking against) and the corresponding objects for wchar etc. in boost_wserialization. So, you need to add -lboost_wserialization to your linker flags.

  • Related