[serialization] duplicate symbols when trying to BOOST_CLASS_EXPORT

Hi, I'm using boost serialization to serialize a bunch of classes, sometimes I need to serialize via a base pointer to a derived class. Let's consider the very simple case where we have a class B deriving from class A. /*** a.h ***/ #ifndef __A__H #define __A__H #include <iostream> class A { private: virtual void make_polymorphic() = 0; friend class boost::serialization::access; template <typename Archive> void serialize(Archive& ar, const unsigned int version) { std::cout << "A!\n"; } }; #endif /*** b.h ***/ #ifndef __B__H #define __B__H #include <boost/serialization/base_object.hpp> #include <boost/serialization/export.hpp> #include "a.h" class B : public A { private: friend class boost::serialization::access; template <typename Archive> void serialize(Archive& ar, const unsigned int version) { ar & boost::serialization::base_object<A>(*this); std::cout << "B!\n"; } virtual void make_polymorphic() {} }; BOOST_CLASS_EXPORT(B); #endif I'm using the BOOST_CLASS_EXPORT so that it registers a GUID for class B, so that serialization of a base pointer to a derived class works. This works fine. My problem is that, if I have 2 files that include b.h, I will get duplicate symbols for B's guid. For example, suppose we add b.cpp with an empty default constructor, just so that we compile another file. #include "b.h" B::B() { } Let's use the following main: // main.cpp #include <iostream> #include <fstream> #include <boost/archive/binary_oarchive.hpp> #include "b.h" int main() { std::ofstream of("mybin.bin"); boost::archive::binary_oarchive oa(of); A* b = new B(); oa << b; return 0; } Which I compile with g++ main.cpp b.cpp -lboost_system -lboost_serialization -o main I get, ld: duplicate symbol boost::archive::detail::extra_detail::init_guid<B>::g If I don't have 2 files, then of course I don't have duplicate symbols and the serialization of the polymorphic object works fine. Any idea how to resolve this issue? Thank you -V

V D wrote:
If I don't have 2 files, then of course I don't have duplicate symbols and the serialization of the polymorphic object works fine.
Any idea how to resolve this issue?
Look into the more recent documentation for EXPORT_KEY and EXPORT_IMPL which addresses this exact issue.
Thank you -V
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

Hi Robert, Indeed the more recent documentation addresses this. http://www.boost.org/doc/libs/1_47_0/libs/serialization/doc/traits.html#expo... So I added BOOST_CLASS_EXPORT_KEY2(B, "B") to my b header and BOOST_CLASS_EXPORT_IMPLEMENT(B) to b's cpp file. I do not get the duplicate symbol anymore, good. However the serialization now crashes. My main is simply: int main() { std::ofstream of("mybin.bin"); boost::archive::binary_oarchive oa(of); A* b = new B(); oa << b; return 0; } Any idea ? Thanks, -V On Dec 2, 2011, at 8:21 AM, Robert Ramey wrote:
V D wrote:
If I don't have 2 files, then of course I don't have duplicate symbols and the serialization of the polymorphic object works fine.
Any idea how to resolve this issue?
Look into the more recent documentation for EXPORT_KEY and EXPORT_IMPL which addresses this exact issue.
Thank you -V
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
participants (2)
-
Robert Ramey
-
V D