0

I've tried to implement an own archive type for boost serialization following official boost example to write archives.

#include <iostream>
#include <vector>

#include <boost/serialization/nvp.hpp>
#include "boost/serialization/vector.hpp"

#include <boost/archive/detail/common_oarchive.hpp>
#include <boost/archive/detail/register_archive.hpp>

#include <boost/archive/detail/archive_serializer_map.hpp>


class complete_oarchive : public boost::archive::detail::common_oarchive<complete_oarchive>
{
    friend class boost::archive::save_access;

    template<class T>
    void save(T & t){
      std::cout << "saved data\n";
    }

public:
    void save_binary(void *address, std::size_t count){
    }
};


template class boost::archive::detail::archive_serializer_map<complete_oarchive>;
template class boost::archive::detail::common_oarchive<complete_oarchive>;
BOOST_SERIALIZATION_REGISTER_ARCHIVE(complete_oarchive)


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

    std::vector<double> testVector = {1, 2, 3, 4};
    complete_oarchive oa;
    std::vector<double>* pVec = &testVector;
    oa << BOOST_SERIALIZATION_NVP(testVector);
    oa << BOOST_SERIALIZATION_NVP(pVec);

    return 0;
}

Compiling this example with

g++    -c -g -std=c++11 -MMD -MP -MF "build/Debug/GNU-Linux/demo.o.d" -o build/Debug/GNU-Linux/demo.o demo.cpp
g++     -o dist/Debug/GNU-Linux/serializationdemo build/Debug/GNU-Linux/demo.o -lboost_serialization

leads to the following linker error

build/Debug/GNU-Linux/demo.o: In function `boost::archive::detail::pointer_oserializer<complete_oarchive, std::vector<double, std::allocator<double> > >::pointer_oserializer()':
/opt/tools/boost/boostRdk-1.66.0/include/boost/archive/detail/oserializer.hpp:222: undefined reference to `boost::archive::detail::archive_serializer_map<complete_oarchive>::insert(boost::archive::detail::basic_serializer const*)'
build/Debug/GNU-Linux/demo.o: In function `boost::archive::detail::pointer_oserializer<complete_oarchive, std::vector<double, std::allocator<double> > >::~pointer_oserializer()':
/opt/tools/boost/boostRdk-1.66.0/include/boost/archive/detail/oserializer.hpp:227: undefined reference to `boost::archive::detail::archive_serializer_map<complete_oarchive>::erase(boost::archive::detail::basic_serializer const*)'
collect2: error: ld returned 1 exit status 

It seems that serializing a pointer in

oa << BOOST_SERIALIZATION_NVP(pVec);

leads to this error. After deletion of this line everything works fine and the result is as expected. Does anybody have experience in writting own serialization archives?

A simimal Problem was solved here https://groups.google.com/forum/#!topic/boost-list/CMoDosGZUo8 but I wasn't able to solve this by forward declarations.

1 Answers1

1

I solved the issue by replacing

#include <boost/archive/detail/archive_serializer_map.hpp>

by

#include <boost/archive/impl/archive_serializer_map.ipp>