提升序列化:链接到库

时间:2011-11-13 23:47:59

标签: c++ macos boost linker

我想使用Boost库序列化程序中的对象。这是我第一次使用它,它给了我一些问题。

在我的程序中,我实现了“序列化”方法并包含以下内容:

#include <fstream>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>

现在,当我构建时,我收到以下错误:

Undefined symbols for architecture x86_64:
  "boost::archive::text_oarchive_impl<boost::archive::text_oarchive>::text_oarchive_impl(std::basic_ostream<char, std::char_traits<char> >&, unsigned int)", referenced from:
      boost::archive::text_oarchive::text_oarchive(std::basic_ostream<char, std::char_traits<char> >&, unsigned int)in Main.o
  "boost::archive::detail::basic_oarchive::~basic_oarchive()", referenced from:
      boost::archive::detail::common_oarchive<boost::archive::text_oarchive>::~common_oarchive()in Main.o
      boost::archive::detail::common_oarchive<boost::archive::text_oarchive>::~common_oarchive()in Main.o
      boost::archive::detail::common_oarchive<boost::archive::text_oarchive>::~common_oarchive()in Main.o
  "boost::archive::basic_text_oprimitive<std::basic_ostream<char, std::char_traits<char> > >::~basic_text_oprimitive()", referenced from:
      boost::archive::text_oarchive_impl<boost::archive::text_oarchive>::~text_oarchive_impl()in Main.o
      boost::archive::text_oarchive_impl<boost::archive::text_oarchive>::~text_oarchive_impl()in Main.o
      boost::archive::text_oarchive_impl<boost::archive::text_oarchive>::~text_oarchive_impl()in Main.o
  "vtable for boost::archive::archive_exception", referenced from:
      boost::archive::archive_exception::archive_exception(boost::archive::archive_exception const&)in Main.o
  "typeinfo for boost::archive::archive_exception", referenced from:
      void boost::serialization::throw_exception<boost::archive::archive_exception>(boost::archive::archive_exception const&)in Main.o
  "boost::archive::archive_exception::~archive_exception()", referenced from:
      void boost::serialization::throw_exception<boost::archive::archive_exception>(boost::archive::archive_exception const&)in Main.o
      void boost::archive::basic_text_oprimitive<std::basic_ostream<char, std::char_traits<char> > >::save<boost::archive::tracking_type>(boost::archive::tracking_type const&)in Main.o
      void boost::archive::basic_text_oprimitive<std::basic_ostream<char, std::char_traits<char> > >::save<boost::archive::object_reference_type>(boost::archive::object_reference_type const&)in Main.o
      void boost::archive::basic_text_oprimitive<std::basic_ostream<char, std::char_traits<char> > >::save<boost::archive::object_id_type>(boost::archive::object_id_type const&)in Main.o
      void boost::archive::basic_text_oprimitive<std::basic_ostream<char, std::char_traits<char> > >::save<unsigned int>(unsigned int const&)in Main.o
      void boost::archive::basic_text_oprimitive<std::basic_ostream<char, std::char_traits<char> > >::save<boost::archive::class_id_reference_type>(boost::archive::class_id_reference_type const&)in Main.o
      void boost::archive::basic_text_oprimitive<std::basic_ostream<char, std::char_traits<char> > >::save<boost::archive::class_id_type>(boost::archive::class_id_type const&)in Main.o
      ...
  "boost::archive::archive_exception::archive_exception(boost::archive::archive_exception::exception_code, char const*, char const*)", referenced from:
      void boost::archive::basic_text_oprimitive<std::basic_ostream<char, std::char_traits<char> > >::save<boost::archive::tracking_type>(boost::archive::tracking_type const&)in Main.o
      void boost::archive::basic_text_oprimitive<std::basic_ostream<char, std::char_traits<char> > >::save<boost::archive::object_reference_type>(boost::archive::object_reference_type const&)in Main.o
      void boost::archive::basic_text_oprimitive<std::basic_ostream<char, std::char_traits<char> > >::save<boost::archive::object_id_type>(boost::archive::object_id_type const&)in Main.o
      void boost::archive::basic_text_oprimitive<std::basic_ostream<char, std::char_traits<char> > >::save<unsigned int>(unsigned int const&)in Main.o
      void boost::archive::basic_text_oprimitive<std::basic_ostream<char, std::char_traits<char> > >::save<boost::archive::class_id_reference_type>(boost::archive::class_id_reference_type const&)in Main.o
      void boost::archive::basic_text_oprimitive<std::basic_ostream<char, std::char_traits<char> > >::save<boost::archive::class_id_type>(boost::archive::class_id_type const&)in Main.o
  "boost::archive::basic_text_oarchive<boost::archive::text_oarchive>::newtoken()", referenced from:
      void boost::archive::text_oarchive_impl<boost::archive::text_oarchive>::save<boost::archive::tracking_type>(boost::archive::tracking_type const&)in Main.o
      void boost::archive::text_oarchive_impl<boost::archive::text_oarchive>::save<boost::archive::object_reference_type>(boost::archive::object_reference_type const&)in Main.o
      void boost::archive::text_oarchive_impl<boost::archive::text_oarchive>::save<boost::archive::object_id_type>(boost::archive::object_id_type const&)in Main.o
      void boost::archive::text_oarchive_impl<boost::archive::text_oarchive>::save<unsigned int>(unsigned int const&)in Main.o
      void boost::archive::text_oarchive_impl<boost::archive::text_oarchive>::save<boost::archive::class_id_reference_type>(boost::archive::class_id_reference_type const&)in Main.o
      void boost::archive::text_oarchive_impl<boost::archive::text_oarchive>::save<boost::archive::class_id_type>(boost::archive::class_id_type const&)in Main.o
  "boost::archive::detail::basic_oarchive::end_preamble()", referenced from:
      void boost::archive::save_access::save_primitive<boost::archive::text_oarchive, boost::archive::tracking_type>(boost::archive::text_oarchive&, boost::archive::tracking_type const&)in Main.o
      void boost::archive::save_access::save_primitive<boost::archive::text_oarchive, boost::archive::object_reference_type>(boost::archive::text_oarchive&, boost::archive::object_reference_type const&)in Main.o
      void boost::archive::save_access::save_primitive<boost::archive::text_oarchive, boost::archive::object_id_type>(boost::archive::text_oarchive&, boost::archive::object_id_type const&)in Main.o
      void boost::archive::save_access::save_primitive<boost::archive::text_oarchive, boost::archive::version_type>(boost::archive::text_oarchive&, boost::archive::version_type const&)in Main.o
      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&)in Main.o
      void boost::archive::save_access::save_primitive<boost::archive::text_oarchive, boost::archive::class_id_reference_type>(boost::archive::text_oarchive&, boost::archive::class_id_reference_type const&)in Main.o
      void boost::archive::save_access::save_primitive<boost::archive::text_oarchive, boost::archive::class_id_type>(boost::archive::text_oarchive&, boost::archive::class_id_type const&)in Main.o
      ...
  "boost::archive::text_oarchive_impl<boost::archive::text_oarchive>::save(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)", referenced from:
      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&)in Main.o
  "typeinfo for boost::archive::detail::basic_oarchive", referenced from:
      typeinfo for boost::archive::detail::common_oarchive<boost::archive::text_oarchive>in Main.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [stable] Error 1

我不确定问题是由于链接还是与64位和32位架构有关。

我尝试过连接:

-L/opt/local/lib/ -lboost_iostreams -lboost_serialization

但它似乎没有任何区别。

2 个答案:

答案 0 :(得分:5)

看起来你的boost库包只有32位;您将不得不重建它添加64位二进制文​​件,或设置您的主项目以匹配boost库。 尝试将项目设置为生成32位二进制而不是64位。在XCode 4.2中,您将使用项目检查器中构建设置的“体系结构”部分。

答案 1 :(得分:0)

在我的情况下:这是因为我使用了编译器gcc6(或gcc5有相同的问题)。我的代码使用不同的编译标志和boost库。详细说明在此link

如果您可以重新编译您使用的所有不兼容的库,请使用编译器选项

-D_GLIBCXX_USE_CXX11_ABI=1

然后重建您的项目。如果仍然无法链接,请将change project的makefile编译器选项添加到0。

-D_GLIBCXX_USE_CXX11_ABI=0