我正在使用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有问题吗?
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 +universal
安装了boost。
答案 0 :(得分:17)
我可以使用以下代码重现该问题:
#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++
链接失败时,基本上会显示错误消息(我已在/opt/boost
安装了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 installation guide构建一个:
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
答案 1 :(得分:0)
看起来你的boost库可能是32位。
这是一个你不会注意到的问题,直到你试图使用少数只有标题的boost工具之一。