|
Boost Users : |
From: Y Fang (yfang00_at_[hidden])
Date: 2007-11-09 21:13:09
Hi,
I am trying to use boost serialization library, but run into link
errors that I don't know how to resolve.
I created a very simple data structure with a 'string' and 'int' in
it. Then I try to serialize it to a stringstream and recover from a
stringstream. The compile failed during linking. The first few errors
are:
% make
g++ -O0 -g3 -Wall -D_REENTRANT -pthread -lboost_program_options
-lboost_filesystem -lboost_iostreams -lboost_thread -lboost_regex
-lboost_serialization -lpthread -o a.out main.o SimpleData.o
main.o: In function `boost::archive::text_oarchive&
boost::smart_cast_impl::reference<boost::archive::text_oarchive&>::polymorphic::cross::cast<boost::archive::detail::basic_oarchive>(boost::archive::detail::basic_oarchive&)':
/usr/include/boost/smart_cast.hpp:76: undefined reference to `typeinfo
for boost::archive::detail::basic_oarchive'
main.o: In function `void
boost::archive::save_access::save_primitive<boost::archive::text_oarchive,
std::basic_string<char, std::char_traits<char>, std::allocator<char> >
>(boost::archive::text_oarchive&, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > const&)':
/usr/include/boost/archive/detail/oserializer.hpp:91: undefined
reference to `boost::archive::detail::basic_oarchive::end_preamble()'
/usr/include/boost/archive/detail/oserializer.hpp:92: undefined
reference to `boost::archive::text_oarchive_impl<boost::archive::text_oarchive>::save(std::basic_string<char,
std::char_traits<char>, std::allocator<char> > const&)'
main.o: In function `void
boost::archive::text_oarchive_impl<boost::archive::text_oarchive>::save<int>(int
const&)':
/usr/include/boost/archive/text_oarchive.hpp:53: undefined reference
to `boost::archive::basic_text_oarchive<boost::archive::text_oarchive>::newtoken()'
main.o: In function `void
boost::archive::save_access::save_primitive<boost::archive::text_oarchive,
int>(boost::archive::text_oarchive&, int const&)':
/usr/include/boost/archive/detail/oserializer.hpp:91: undefined
reference to `boost::archive::detail::basic_oarchive::end_preamble()'
main.o: In function `void
boost::archive::text_oarchive_impl<boost::archive::text_oarchive>::save<boost::archive::class_id_reference_type>(boost::archive::class_id_reference_type
const&)':
/usr/include/boost/archive/text_oarchive.hpp:53: undefined reference
to `boost::archive::basic_text_oarchive<boost::archive::text_oarchive>::newtoken()'
......
I attached the source code of the 4 files I use: main.cpp,
SimpleData.h, SimpleData.cpp, and Serialize.cpp. Also, here is the g++
version I use:
% g++ -v
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v
--enable-languages=c,c++,fortran,objc,obj-c++,treelang --prefix=/usr
--enable-shared --with-system-zlib --libexecdir=/usr/lib
--without-included-gettext --enable-threads=posix --enable-nls
--program-suffix=-4.1 --enable-__cxa_atexit --enable-clocale=gnu
--enable-libstdcxx-debug --enable-mpfr --enable-checking=release
i486-linux-gnu
Thread model: posix
gcc version 4.1.2 (Ubuntu 4.1.2-0ubuntu4)
Is there something I missed in the include list? I tried '#include
<boost/archive/detail/basic_oarchive.hpp>' in Serialize.h file, that
would trigue compile error even before linking.
Thanks,
Yu
main.cpp:
#include <string>
#include <iostream>
#include "SimpleData.h"
#include "Serialize.h"
int main ()
{
// test serialization of data structure
SimpleData sd("hello", 1);
std::string sdstr = saveData<SimpleData>(sd);
std::cout << "Serialized simple data is:\n" << sdstr << std::endl;
return 0;
}
SimpleData.h:
#ifndef SIMPLEDATA_H_
#define SIMPLEDATA_H_
#include <string>
class SimpleData
{
public:
SimpleData();
SimpleData(std::string id_, int code_);
virtual ~SimpleData();
std::string id;
int code;
};
#endif /*SIMPLEDATA_H_*/
SimpleData.cpp:
#include "SimpleData.h"
SimpleData::SimpleData ()
{
id = "default";
code = 0;
return;
}
SimpleData::SimpleData (std::string id_, int code_) :
id(id_), code(code_)
{
return;
}
SimpleData::~SimpleData ()
{
return;
}
Serialize.h:
#ifndef SERIALIZE_H_
#define SERIALIZE_H_
#include <sstream>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/base_object.hpp>
#include <boost/serialization/utility.hpp>
#include <boost/serialization/string.hpp>
#include "SimpleData.h"
namespace boost {
namespace serialization {
// add a serialize function below for each data
structure that needs
// to be serialized
// serialize simple data
template<class Archive>
void serialize (Archive & _ar, SimpleData &
_sd, const unsigned int _version)
{
_ar & _sd.id;
_ar & _sd.code;
}
}
}
// serialize data structure and save into a string
// note: there must be a serialize function defined
// above for the data structure
template<class Data>
std::string saveData (const Data& _d)
{
std::ostringstream oss;
boost::archive::text_oarchive oa(oss);
oa << _d;
return oss.str();
}
// deserialize and recover data structure from a string
template<class Data>
Data loadData (const std::string& _s)
{
std::istringstream iss(_s);
boost::archive::text_iarchive ia(iss);
Data d;
ia >> d;
return d;
}
#endif /*SERIALIZE_H_*/
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