Opened 12 years ago

Closed 9 years ago

Last modified 9 years ago

#5368 closed Patches (invalid)

Item for "Tips and Tricks" list in dokumentation

Reported by: schorsch_76@… Owned by: Robert Ramey
Milestone: To Be Determined Component: serialization
Version: Boost Release Branch Severity: Optimization
Keywords: Boost 1.46.1 Cc:

Description

How to handle BOOST_CLASS_EXPORT_KEY/BOOST_CLASS_EXPORT_IMPLEMENT in several static libraries and avoid linking errors?

Make Header Archive.h

Make a Header "Archive.h". Include Everything for your serialization here. Example:

Code highlighting: :

#ifndef __Archive_h__
#define __Archive_h__

// STL Archive + stuff
#include <boost/serialization/export.hpp>
#include <boost/serialization/base_object.hpp> 
#include <boost/serialization/list.hpp>
#include <boost/serialization/map.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/serialization/shared_ptr.hpp>

// IMPORTANT: Archive Headers at last
// include headers that implement a archive in xml format
#include <boost/archive/archive_exception.hpp>

#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>

typedef boost::archive::xml_oarchive oarchive;
typedef boost::archive::xml_iarchive iarchive;

#endif // __Archive_h__

Define your classes as following

Myclass.h

Code highlighting: :


#include "Archive.h"

class Myclass
{
public:
        Myclass();
        ~Myclass();

        double md_data;

private:
    friend class boost::serialization::access;

    // When the class Archive corresponds to an output archive, the
    // & operator is defined similar to <<.  Likewise, when the class Archive
    // is a type of input archive the & operator is defined similar to >>.
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
                ar & BOOST_SERIALIZATION_NVP(md_data;);
    }
};

BOOST_CLASS_VERSION(Myclass, 0)
BOOST_CLASS_EXPORT_KEY(Myclass)

Implementationfile of your class

Myclass.cpp

Code highlighting: :


Myclass::Myclass()
{
}

Myclass::~Myclass()
{
}
BOOST_CLASS_EXPORT_IMPLEMENT(Myclass) 

Conclusion

Thats the way i made it working in my project with VC10 and boost 1.46.1 . My project consists of several static libraries which have dependencies under each other. Main dependency is the library which contains the serializable classes.

Hopefully this will help some people :-)

Best Regards

Georg

Change History (5)

comment:1 by Aaron Barany <akb825@…>, 12 years ago

Agreed, I needed to do this for our usage of serialization as well.

comment:2 by alvarolb@…, 10 years ago

two days with linker problems! finally I can get it working! thx!!

comment:3 by Robert Ramey, 9 years ago

I don't get this - I need a better explanation of the problem. I see that some other users have found this useful. Is this information not already in the documentation? Is there but confusing. Need a more specific explanation.

Robert Ramey

comment:4 by Robert Ramey, 9 years ago

Resolution: invalid
Status: newclosed

comment:5 by schorsch_76@…, 9 years ago

Hello Robert,

Yes, that is already in the documentation, but at that time i was relativly new to boost::serialization and it was not easy to get a working solution for me, so i added this ticket to boost to optimize the documentation for a starting point to explain to a new user the usage of BOOST_CLASS_EXPORT_KEY/BOOST_CLASS_EXPORT_IMPLEMENT.

The problem were at start, as pointed out by comment 1 and 2, the linker problems. It's not a bug of the library, but of its usage of course. This simple example showed how to stuff it together to work around these linker problems. For you as inventor/developer of boost::serialization it's absolutly clear, but not for all of us ;) What's hard, is that the order of the include files are important.

Thats my point.

Bye Georg

Note: See TracTickets for help on using tickets.