问题
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
I am serializing an std::vector<std::string>
:
boost::archive::text_oarchive oa(std::cout);
oa << tasks;
Is there a problem with my installation of boost?
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
I installed boost using sudo port install boost +universal
on Mac OS X 10.7.
回答1:
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;
}
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
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
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
回答2:
It looks like your boost libraries may be 32 bit.
That's a problem you wouldn't notice until you tried to use one of the few boost utils that are not header-only.
来源:https://stackoverflow.com/questions/8728742/linker-errors-when-using-boost-serialization