使用boost序列化时链接器错误

我正在使用boost序列化。 我编译了: -L/opt/local/lib -lboost_serialization -stdlib=libc++ ,但得到了几个(不可更改的)错误:

Undefined symbols for architecture x86_64:
  "boost::archive::text_oarchive_impl::save(std::__1::basic_string, std::__1::allocator > const&)", referenced from:
      void boost::archive::save_access::save_primitive, std::__1::allocator > >(boost::archive::text_oarchive&, std::__1::basic_string, std::__1::allocator > const&) in main.o
  "boost::archive::basic_text_oprimitive > >::~basic_text_oprimitive()", referenced from:
      boost::archive::text_oarchive_impl::~text_oarchive_impl() in main.o
  "boost::archive::text_oarchive_impl::text_oarchive_impl(std::__1::basic_ostream >&, unsigned int)", referenced from:
      boost::archive::text_oarchive::text_oarchive(std::__1::basic_ostream >&, unsigned int) in main.o
ld: symbol(s) not found for architecture x86_64

我序列化一个std::vector<std::string>

boost::archive::text_oarchive oa(std::cout);
oa << tasks;

我的升级安装有问题吗?


boost库是包含32位和64位机器码的通用二进制文件(所以这不是我想的问题):

$ file libboost_serialization.dylib
libboost_serialization.dylib: Mach-O universal binary with 2 architectures
libboost_serialization.dylib (for architecture i386): Mach-O dynamically linked shared library i386
libboost_serialization.dylib (for architecture x86_64): Mach-O 64-bit dynamically linked shared library x86_64

我在Mac OS X 10.7上使用sudo port install boost +universalsudo port install boost +universal


我可以使用下面的代码重现该问题:

#include "boost/archive/text_oarchive.hpp"
#include "boost/serialization/vector.hpp"
#include <vector>
#include <string>

int main()
{
    std::vector<std::string> tasks;
    boost::archive::text_oarchive oa(std::cout);
    oa << tasks;
}

当使用g++clang++及其相应的默认标志并链接-lboost_serialization时,这种编译和链接没有问题。 然而,当使用clang++libc++链接失败时,本质上是错误消息引用(我已将Boost安装在/opt/boost ):

clang++ -c -stdlib=libc++ -I/opt/boost -W -Wall -ansi serialize.cpp
clang++ -o serialize.tsk -L/opt/boost/stage/lib -stdlib=libc++ serialize.o -lboost_serialization

基于此,我认为使用-stdlib = libc ++的构建需要基于Boost安装指南使用自己的Boost构建版本并构建一个构建版本:

tar jxvf ~/Downloads/boost_1_48_0.tar.bz2
cd boost_1_48_0/tools/build/v2

# change the build rules to use -stdlib=libc++:
mv tools/clang-darwin.jam tools/clang-darwin.jam.orig
sed -e 's/CONFIG_COMMAND)"/CONFIG_COMMAND)" -stdlib=libc++/' < tools/clang-darwin.jam.orig > tools/clang-darwin.jam

./boostrap.sh
sudo ./b2 install --prefix=/opt/boost-clang
cd ../../..
/opt/boost-clang/bin/b2 --build-dir=/opt/tmp toolset=clang stage
sudo /opt/boost-clang/bin/b2 --build-dir=/opt/tmp toolset=clang install --prefix=/opt/boost-clang

我对clang-darwin.jam所做的编辑几乎肯定不是那些打算的,但他们似乎在做诡计:我对“bjam”了解不多,而我试图找到一个合适的位置来应用更改。 安装的某些步骤使用sudo将东西安装到受保护的目录中。 显然,你可以安装到你有写权限的其他目录中。 我只是在我的机器上安装了一些东西,防止我意外地搞乱了它。

但是,通过安装,我可以成功构建该程序:

/opt/llvm/bin/clang++ -stdlib=libc++ -W -Wall -ansi -I/opt/boost-clang  -c -o serialize.o serialize.cpp
/opt/llvm/bin/clang++ -stdlib=libc++ -L/opt/boost-clang/lib serialize.o -lboost_serialization -o serialize.tsk

它看起来像你的增强库可能是32位。

这是一个你不会注意到的问题,直到你尝试使用其中一个不是标题的几个boost utils。

链接地址: http://www.djcxy.com/p/62913.html

上一篇: Linker errors when using boost serialization

下一篇: How to set the same image for iPhone Retina Display and iPad non Retina