Boost logo

Boost Users :

Subject: Re: [Boost-users] [serialization] problem using BOOST_CLASS_EXPORT macro in a static library
From: Robert Ramey (ramey_at_[hidden])
Date: 2009-02-10 14:38:27


The problem is that the exported type is not explicitly referred to in the
code
so the implemenation doesn't get pulled out of the static library. After
all
the purpose of using a static library is to make sure that your executable
ONLY contains code that it is known (at compile time) to be required.

There are a couple of ways to do this.

a) include an "export.cpp" which includes all your EXPORT macros
b) include them in a DLL and load the DLL at execute time. Or
c) include one class/function in your DLL which you KNOW will
be referred to at execution time
c) use explicit registration of derived types - thereby avoiding
EXPORT all together

Basically ALL of these methods amount to advising your
executable of the types you plan to serialize before you actually
serialize them. This can happen automagially in some cases
(e.g. c) above) or explicitly.

Robert Ramey

Renaud Lepere wrote:
> I have some problems with BOOST_CLASS_EXPORT macro, it
> seems that this macro have no effect if i put it in
> a separate .cpp file of a static library.
>
> I have a simple class Element that contains a pointer to a
> curve that can be of different types (geom2d.hpp).
>
> struct Curve
> {
> Curve() {};
> virtual ~Curve() {};
> };
> struct Line : public Curve
> {
> Line(int y = 0): m_y(y) {};
> int m_y;
> };
>
> typedef boost::shared_ptr< Curve > CurvePtr;
> struct Element
> {
> Element(const CurvePtr & curve):
> m_curve(curve) {}
> boost::shared_ptr< Curve > m_curve;
> };
>
> I would like to be able to serialize Element, so i
> wrote a serialize.hpp file (attached) containing code for
> serializing those structures.
>
> I also wrote export.cpp using
>
> #include "geom2d.hpp"
> #include <boost/archive/text_oarchive.hpp>
> #include <boost/archive/text_iarchive.hpp>
> #include "serialize.hpp"
> #include <boost/serialization/export.hpp>
> #include <iostream>
>
> BOOST_CLASS_EXPORT(Curve);
> BOOST_CLASS_EXPORT(Line);
>
> I put the export.cpp file in a library named geom2d,
> but when trying to serialize my structures using something
> (attached main.cpp).
>
> CurvePtr c(new Line(10));
> const Element e(c);
> std::ofstream ofs("filename");
> {
> boost::archive::text_oarchive oa(ofs);
> oa << e;
> }
> I received an exception "unregistered class". If i dont
> put export.cpp in a library but in the same compilation unit
> as main.cpp, the error disappear (visual studio 8.0 and 9.0).
>
> What is the good way of doing this, should i wrote an export.hpp file
> containing the macros and include this file in a .cpp of the main
> compilation unit ?
>
> Thanks,
>
> Renaud
>
>
>
>
>
>> _______________________________________________
>> Boost-users mailing list
>> Boost-users_at_[hidden]
>> http://lists.boost.org/mailman/listinfo.cgi/boost-users


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net