Saikumar Kusuma
Saikumar Kusuma

Reputation: 47

undefined reference to boost serialization functions

and using boost serialization I have been getting a lot of errors using the serialization header files, like this:

/home/saikumar/Downloads/boost_1_61_0/boost/archive/text_iarchive.hpp:117: error: undefined reference to `boost::archive::text_iarchive_impl::text_iarchive_impl(std::istream&, unsigned int)'

I tried every possible links in stackoverflow (1, 2, 3, and several others).

They say to use LIBS += -lboost_system or others but boost documentation says it is a header only library Boost setup, which means it doesn't require any library linkage and even if I try searching for that library in boost folder, I can not find it.

I followed the same steps performed in boost setup process but I am not able to find any thing. Here is the code that i am testing it on:

#include <fstream>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/serialization/string.hpp>
#include <boost/serialization/vector.hpp>
#include <vector>

class test
{
public:

    std::vector<int> test1;
    std::string test2;

    test()
    {
    }

    test(std::vector<int> h, std::string i):test1(h), test2(i)
    {
    }
private:
    friend class boost::serialization::access;
    template<class Archive> void serialize(Archive & ar, const unsigned int version)
    {
        ar & test1;
        ar & test2;
    }
};

int main(int argc, char *argv[])
{
    test obj;

    obj.test1.push_back(1);
    obj.test1.push_back(3);
    obj.test1.push_back(6);
    obj.test1.push_back(74);
    obj.test1.push_back(43);
    obj.test1.push_back(12);
    obj.test1.push_back(8);
    obj.test1.push_back(13);

    obj.test2 ="jsahdjfkadshflkssfjdslfk";


    std::ofstream file("myfile.FILE");
    boost::archive::text_oarchive oo(file);
    oo<<obj;

    test obj1;

    std::ifstream ifs("myfile.FILE");
    boost::archive::text_iarchive ia(ifs);
    ia >> obj1;

    return 0;
}

Upvotes: 2

Views: 6160

Answers (2)

Lebohang Mbele
Lebohang Mbele

Reputation: 3758

You need to link against boost::serialization. Add -lboost_serialization to your LIBS.

LIBS += -lboost_serialization

Upvotes: 2

Jan Hudec
Jan Hudec

Reputation: 76376

boost/archive/text_iarchive.hpp is part of Boost.Serialization and that definitely is not a header-only library.

The library is normally called boost_serialization (libboost_serialization.so), plus you also need boost_wserialization (libboost_wserialization.so) if you want to use wchar_t with it.

Upvotes: 3

Related Questions