6

我首先要说我今晚感觉自己像个白痴。我试图通过 Boost 序列化最小化重现我在 RHEL5.6、gcc 4.1.2、boost 1.44.0 下遇到的工作问题。

我遇到这个问题的环境是 Ubuntu Server(安装了开发包)、gcc 4.7.2 和 boost 1.44.0 的构建(针对系统编译器、包等)。

我的代码编译得很干净,但我得到了无数与boost::archive各种类型相关的未定义符号。在我的 make 上运行 strace,我看到它获取了预期的 boost_serialization 库:

668 43569 stat("/usr/local/boost/1.44.0/lib/libboost_serialization.so", {st_mode=S_IFREG|0755, st_size=700481, ...}) = 0
669 43569 open("/usr/local/boost/1.44.0/lib/libboost_serialization.so", O_RDONLY) = 8

我的makefile是:

default: test-app
all: test-app

BOOST := /usr/local/boost/1.44.0

CPPFLAGS := -fPIC -Wall -Wextra -Werror
INCDIRS := -isystem$(BOOST)/include
.LIBDIRS. := $(BOOST)/lib
.LIBS. :=boost_serialization
LIBS := $(foreach lib,$(.LIBS.),-l$(lib))
LIBDIRS := $(foreach dir,$(.LIBDIRS.),-L$(dir))
CPPFLAGS += $(INCDIRS)

base.o : base.cpp base.hpp
        g++ $(CPPFLAGS) --compile $< -o $@ -g

derived.o : derived.cpp base.hpp derived.hpp
        g++ $(CPPFLAGS) --compile $< -o $@ -g

main.o: main.cpp derived.hpp
        g++ $(CPPFLAGS) --compile $< -o $@ -g

test-app: main.o derived.o base.o
        g++ -o $@ $(LIBDIRS) $(LIBS) $^

clean:
        rm -f *.o test

我得到的链接器错误的一个(非常)小样本如下:

base.o:在函数void boost::archive::basic_text_oprimitive<std::ostream>::save<boost::archive::class_id_reference_type>(boost::archive::class_id_reference_type const&)': /usr/local/boost/1.44.0/include/boost/archive/basic_text_oprimitive.hpp:91: undefined reference to boost::archive::archive_exception::archive_exception(boost::archive::archive_exception::exception_code, char const*, char const*)' /usr/local/boost/1.44.0/include/ boost/archive/basic_text_oprimitive.hpp:91: 未定义对 boost::archive::archive_exception::~archive_exception()' /usr/local/boost/1.44.0/include/boost/archive/basic_text_oprimitive.hpp:91: undefined reference to boost::archive::archive_exception::~archive_exception()' 的引用 base.o: 在函数void boost::archive::basic_text_oprimitive<std::ostream>::save<boost::archive::tracking_type>(boost::archive::tracking_type const&)': /usr/local/boost/1.44.0/include/boost/archive/basic_text_oprimitive.hpp:91: undefined reference to boost::archive::archive_exception::archive_exception(boost::archive:: archive_exception::exception_code, char const*, char const*)' /usr/local/boost/1.44.0/include/boost/archive/basic_text_oprimitive.hpp:91: 未定义对 boost::archive::archive_exception::~archive_exception()' /usr/local/boost/1.44.0/include/boost/archive/basic_text_oprimitive.hpp:91: undefined reference to boost::archive::archive_exception::~的引用archive_exception()' base.o:在函数中void boost::archive::basic_text_oprimitive<std::ostream>::save<unsigned int>(unsigned int const&)': /usr/local/boost/1.44.0/include/boost/archive/basic_text_oprimitive.hpp:91: undefined reference to boost::archive::archive_exception::archive_exception(boost::archive::archive_exception::exception_code, char const*, char const*)' /usr/local/boost/1.44.0/include/boost/archive/basic_text_oprimitive. hpp:91: 未定义对 boost::archive::archive_exception::~archive_exception()' /usr/local/boost/1.44.0/include/boost/archive/basic_text_oprimitive.hpp:91: undefined reference to boost::archive::archive_exception::~archive_exception()' 的引用

同样,如果我对引用的 .so 进行符号转储,我会看到所需的符号:

objdump -t /usr/local/boost/1.44.0/lib/libboost_serialization.so | c++过滤器| grep "boost::archive::archive_exception::~archive_exception()" 000000000004e670 g F .text 0000000000000065
boost::archive::archive_exception::~archive_exception() 000000000004e6e0 g F .text 000000000000000 虚拟到 boost:archive:9 Archive_exception :: 〜Archive_exception()00000000000004E6F0 G F .TEXT 0000000000000012
BOOST :: Archive :: Archive_exception :: 〜Archive_exception(
〜Archive_exception(〜Archive_exception() 0000000000000009 虚拟 thunk 到 boost::archive::archive_exception::~archive_exception()

我已经把头撞在桌子上一段时间了……希望有人能帮忙。我不认为具体的来源很重要,但如果需要,我可以发布它。

其他环境细节:

g++ --version g++ (Ubuntu/Linaro 4.7.2-2ubuntu1) 4.7.2 版权所有 (C) 2012 Free Software Foundation, Inc. 这是免费软件;查看复制条件的来源。没有保修;甚至不考虑适销性或特定用途的适用性。

ld --version
GNU ld (GNU Binutils for Ubuntu) 2.22.90.20120924
Copyright 2012 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License version 3 or (at your option) a later version.
This program has absolutely no warranty.

此外,这是在 Windows 8 上的 Hyper-V 下运行的 VM 上,不过,我认为这在这里并不重要。

完整的链接行: g++ -L/usr/local/boost/1.44.0/lib -lboost_serialization -o test-app main.o derived.o base.o

4

2 回答 2

8

我遇到了同样的问题,把 -lboost_serialization 放在最后

g++ -L/usr/local/boost/1.44.0/lib -o test-app main.o derived.o base.o -lboost_serialization

应该解决它

于 2013-05-29T11:47:51.960 回答
-1

二进制对象 .o 未与库链接。
首先验证/usr/local/boost/1.44.0/lib中有文件的 lib boost_serialization。
接下来,注册该库:
sudo ldconfig -v | grep boost_serialization
如果没有,则使用 root 用户在/etc/ld.so.conf.d中创建文件boost1.44.0.conf 。使用这个 usr/local/boost/1.44.0/lib 然后启动这个命令 sudo ldconfig -v | grep boost_serialization


于 2013-02-27T05:56:43.260 回答