Hi everybody,
I have just tried a simple example of a class serialization in a DLL and did not
manage to make it work. I have been using Boost serialization since a while but
until now I was putting all the serialization code in the .hpp. By the way
thanks Robert (and Boost) for this great library.
I am using MSVC 8.0 and Boost 1.34.1. I have tried the same with Boost
Serialization 1.36 (head of Boost) without any success.
My test is the following. The class MyClass in declared in a DLL
boost-example.dll built from Example.cpp. A test program Example.t.cpp linked
with boost-example.dll try to serialize MyClass to an XML archive.
The files are the following:
//////// Example.hpp
#if _MSC_VER > 1000
#pragma once
#endif
// Do NOT include any headers after this point #if
(defined(__COMPILING_Example_CPP__) && defined(WIN32)) #define _LIBSPEC
__declspec(dllexport) #else #define _LIBSPEC #endif
class _LIBSPEC MyClass
{
public:
MyClass();
private:
friend class boost::serialization::access;
template <class Archive>
void serialize(Archive & ar, const unsigned int version);
std::string m_str;
};
//////// Example.cpp
#include <string>
#include "boost/archive/xml_oarchive.hpp"
#include "boost/archive/xml_iarchive.hpp"
#include "boost/serialization/nvp.hpp"
#include "boost/serialization/export.hpp"
#include "Example.hpp"
using namespace std;
BOOST_CLASS_EXPORT(MyClass); // should explicity instantiate template for
// xml_oarchive and xml_iarchive
MyClass::MyClass()
: m_str("MyClass") {}
template <class Archive>
void MyClass::serialize(Archive & ar, const unsigned int version) {
ar & BOOST_SERIALIZATION_NVP(m_str);
}
//////// Example.t.cpp
#include <iostream>
#include <fstream>
#include "boost/archive/xml_oarchive.hpp"
#include "boost/serialization/nvp.hpp"
#include "Example.hpp"
using namespace std;
int main(int argc, char* argv[])
{
MyClass myClass;
// try to serialize MyClass using an XML archive
ofstream ofs("myClass.xml");
if( !ofs ) {
cout << "ERROR: Could not open myClass.xml for writing";
return EXIT_FAILURE;
}
try {
boost::archive::xml_oarchive oa(ofs);
oa << BOOST_SERIALIZATION_NVP(myClass);
} catch (const std::exception& e) {
cout << "failed to serialize MyClass with xml_oarchive: " << e.what();
ofs.setstate(ios::failbit);
}
return EXIT_SUCCESS;
}
When compiling this, I got an error at link time for Example.t:
Example.t.obj : error LNK2019: unresolved external symbol "private: void __thisc
all MyClass::serialize<class boost::archive::xml_oarchive>(class boost::archive:
:xml_oarchive &,unsigned int)" (??$serialize@Vxml_oarchive@archive@boost@@@MyCla
ss@@AAEXAAVxml_oarchive@archive@boost@@I@Z) referenced in function "public: stat
ic void __cdecl boost::serialization::access::serialize