使用Boost序列化时的链接器错误 [英] Linker errors when using boost serialization
问题描述
我正在使用Boost序列化.我使用以下代码进行编译:-L/opt/local/lib -lboost_serialization -stdlib=libc++
,但出现了几个(不透明)错误:
I am using boost serialization. I compiled with: -L/opt/local/lib -lboost_serialization -stdlib=libc++
, but got several (ungooglable) errors:
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是否存在问题?
Is there a problem with my installation of boost?
boost库是包含32位和64位机器代码的通用二进制文件(所以我猜这不是问题):
The boost libraries are universal binaries containing both 32-bit and 64-bit machine code (so that's not the problem I guess):
$ 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.
I installed boost using sudo port install boost +universal
on Mac OS X 10.7.
推荐答案
我可以使用以下代码重现该问题:
I could reproduce the problem using the following code:
#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):
This compiles and links without problems when using g++
or clang++
with their respective default flags and linking with -lboost_serialization
. However, when using clang++
with libc++
linking fails with essentially the error messages quote (I have Boost installed at /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构建,并基于
Based on this I assumed that a build with -stdlib=libc++ wants to have its own Boost build and build one using based on the 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
将内容安装到受保护的目录中.显然,您可以将其安装到具有写权限的其他目录中.我只是在机器上安装了东西,以防止意外将其弄乱.
The edits I made to clang-darwin.jam
are almost certainly not those intended but they seem to do the trick: I don't know much about "bjam" and I just tried to find an appropriate location to apply the change. Some step of the installation use sudo
to install things into protected directories. Obviously, you can install into some other directory where you have write permissions as well. I just installed things on my machine in a way preventing me from accidentally messing it up.
但是,完成此安装后,我可以成功构建程序:
However, with this installation in place I could successfully build the program:
/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
这篇关于使用Boost序列化时的链接器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!