c++ - 使用 Boost.Build 和 Boost.Test - 链接器错误

标签 c++ boost linker boost-test boost-build

我一直在试图弄清楚为什么我的测试无法链接。我在 Ubuntu 上使用 Boost 1.59 的 Boost.Build 和 Boost.Test。我从源代码构建并安装了 boost 库。

我正在指定 <library>/boost//unit_test_framework作为单元测试使用要求的一部分。我的理解是,这应该设置所有链接器要求,以便与 Boost.Test 链接。

不幸的是,即使链接命令指定了 -lboost_unit_test_framework,我也会收到一堆链接器错误,因为 undefined reference 了 Boost.Test 组件 test.o 之后文件

我有一个非常简单的示例,展示了所描述的行为:

Jamroot :

if $(BOOST_ROOT)
{
    use-project /boost : $(BOOST_ROOT) ;
}

import boost ;
boost.use-project ;

using testing ;

unit-test test
    : test.cpp
    : <library>/boost//unit_test_framework
    ;

test.cpp :

#define BOOST_TEST_MODULE test
#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_CASE (pass) {

}

正在运行 b2 -d+2给我使用的编译和链接器命令。

$ b2 -a -d+2
gcc.compile.c++ bin/gcc-4.9.2/debug/test.o

    "g++"  -ftemplate-depth-128 -O0 -fno-inline -Wall -g -std=c++11 -fPIC  -DBOOST_ALL_NO_LIB -DBOOST_TEST_DYN_LINK   -c -o "bin/gcc-4.9.2/debug/test.o" "test.cpp"

gcc.link bin/gcc-4.9.2/debug/test

    "g++"    -o "bin/gcc-4.9.2/debug/test" -Wl,--start-group "bin/gcc-4.9.2/debug/test.o"  -Wl,-Bstatic  -Wl,-Bdynamic -lboost_unit_test_framework -Wl,--end-group -g 

bin/gcc-4.9.2/debug/test.o: In function `__static_initialization_and_destruction_0(int, int)':
/home/anthony/Development/example/test.cpp:4: undefined reference to `boost::unit_test::ut_detail::auto_test_unit_registrar::auto_test_unit_registrar(boost::unit_test::test_case*, boost::unit_test::decorator::collector&, unsigned long)'
bin/gcc-4.9.2/debug/test.o: In function `boost::unit_test::make_test_case(boost::function<void ()> const&, boost::unit_test::basic_cstring<char const>, boost::unit_test::basic_cstring<char const>, unsigned long)':
/usr/local/include/boost/test/tree/test_unit.hpp:249: undefined reference to `boost::unit_test::test_case::test_case(boost::unit_test::basic_cstring<char const>, boost::unit_test::basic_cstring<char const>, unsigned long, boost::function<void ()> const&)'
collect2: error: ld returned 1 exit status
...skipped <pbin/gcc-4.9.2/debug>test.passed for lack of <pbin/gcc-4.9.2/debug>test...
...failed updating 1 target...

最佳答案

even though the link command is specifying -lboost_unit_test_framework after the test.o

注意配合使用

-Wl,--start-group ...test.o  -lboost_unit_test_framework -Wl,--end-group

test.o 和库的顺序无关紧要:因为--{start,end}-group,它们被认为是同一“bookshelf”的一部分。

必须 libboost_unit_test.{a,so} 提供您需要的符号。也许它是用不同版本的 g++ 构建的? (This answer 可能是相关的。)

您的第一步应该是找出您丢失的符号的错位名称:将 -Wl,--no-demangle 添加到您的链接行。

然后您可以验证您确实缺少所需的符号:

nm -A libboost_unit_test.* | grep missing_mangled_symbol

最后,您需要了解为什么您缺少该符号。是否定义了相似但略有不同的符号?此命令可能会提供一些线索:

nm -AC libboost_unit_test.* | grep 'boost::unit_test::test_case::test_case'

更新:

我刚刚发现,不仅 g++ 的版本在这里很重要,而且编译标志也很重要。考虑:

// foo.cc
#include <string>
void foo(const std::string &) { }

// t.cc
#include <string>
void foo(const std::string &);
int main() { std::string s; foo(s); }

使用最近的 GCC 主干:

g++ -c t.cc foo.cc
g++ t.o foo.o  # success

g++ -D_GLIBCXX_USE_CXX11_ABI=0 t.cc foo.o
/tmp/ccKqZVvh.o: In function `main':
t.cc:(.text+0x1d): undefined reference to `foo(std::string const&)'
collect2: error: ld returned 1 exit status

关于c++ - 使用 Boost.Build 和 Boost.Test - 链接器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32420692/

相关文章:

c++ - 嵌入 Python : How to help the scripter?

C++ 编译器或链接器优化

c++ - ld : duplicate symbol

c++ - 从类定义中的外部文件访问函数

c++ - 我可以使用 std::shared_ptr 而不是 boost::shared_ptr 构建 boost 库吗

c++ - 静态数据成员

c++ - boost最短路径查找算法

c++ - 我可以访问一般传递到模板中的结构吗?

c++ - 对于 C++ 中的日期/时间库,是否有 Boost 的轻量级替代方案?

c++ - 通过 ar 链接后专门的成员函数丢失