使用Boost.Build和Boost.Test - 链接错误 [英] Using Boost.Build and Boost.Test - linker errors
问题描述
我卡试图弄清楚为什么我的测试将不会链接。我在Ubuntu使用Boost.Build和Boost.Test与Boost 1.59。我建立并安装从源代码Boost库。
我指定<库> /升压// unit_test_framework
作为单元测试的使用要求的一部分。我的理解是,这应该设置所有的连接要求,以便与Boost.Test链接。
不幸的是,我得到来Boost.Test组件未定义的引用链接错误一堆的即使链接命令指定 -lboost_unit_test_framework
的之后的 test.o
文件
我有一个非常简单的例子,表现出描述的行为:
在 Jamroot中
:
如果$(BOOST_ROOT)
{
使用项目/升压:$(BOOST_ROOT);
}进口提振;
boost.use项目;使用测试;单元测试测试
:TEST.CPP
:LT;库> /升压// unit_test_framework
;
在 TEST.CPP
:
的#define BOOST_TEST_MODULE测试
#包括LT&;升压/测试/ unit_test.hpp>BOOST_AUTO_TEST_CASE(通){}
运行 B2 -d + 2
给我所使用的编译和链接器的命令。
$ B2 -a -d + 2
gcc.compile.c ++斌/ GCC-4.9.2 /调试/ test.o G ++-ftemplate深入-128 -O0 -fno内联-Wall -g -std = C ++ 11 -fPIC -DBOOST_ALL_NO_LIB -DBOOST_TEST_DYN_LINK -c -o斌/ GCC-4.9.2 /调试/ test.o TEST.CPPgcc.link斌/ GCC-4.9.2 /调试/测试 G ++-o斌/ GCC-4.9.2 /调试/测试轮候册, - 启动组的bin / GCC-4.9.2 /调试/ test.o-Wl,-Bstatic轮候册, - Bdynamic -lboost_unit_test_framework轮候册, - 端基-g斌/ GCC-4.9.2 /调试/ test.o:在功能`__static_initialization_and_destruction_0(INT,INT):
/home/anthony/Development/example/test.cpp:4:未定义引用`boost::unit_test::ut_detail::auto_test_unit_registrar::auto_test_unit_registrar(boost::unit_test::test_case*,提高:: unit_test ::装饰::收集和放大器;,无符号长)
斌/ GCC-4.9.2 /调试/ test.o:在函数'的boost :: unit_test :: make_test_case(升压::功能<无效()>常量和放大器;,提振:: unit_test :: basic_cstring<字符常量>中提高:: unit_test :: basic_cstring<字符常量>中无符号长):
/usr/local/include/boost/test/tree/test_unit.hpp:249:未定义的引用`的boost :: unit_test :: test_case :: test_case(升压:: unit_test :: basic_cstring<字符常量>中的boost :: unit_test :: basic_cstring<字符常量>中无符号长,提振::函数<无效()>常量和放大器;)
collect2:错误:LD返回1退出状态
...跳过< PBIN / GCC-4.9.2 /调试> test.passed的不足和LT; PBIN / GCC-4.9.2 /调试>测试...
...未能更新1目标...
即使链接命令在test.o之后指定-lboost_unit_test_framework
块引用>请注意,与使用
轮候册, - 启动组... test.o -lboost_unit_test_framework轮候册, - 端基
test.o
的顺序和图书馆的不的事:因为- {开始,结束} -group
,它们被认为是同一个书架的一部分。它的必须的是,在
libboost_unit_test {A,所以}
并的不的提供您所需要的符号。也许这是一个不同的版本G ++
建? (这个答案可能有关。)您的第一步应该是符号(S)你缺少的重整名称搞清楚:添加
轮候册, - 不还原函数
来你的链接线然后,你可以验证你实际上缺少所需的符号(S):
纳米-A libboost_unit_test。* | grep的missing_mangled_symbol
最后,你需要了解的为什么的您缺少的象征。是否有一个类似但略有不同的符号界定?此命令可以提供一些线索:
纳米-AC libboost_unit_test。* | grep的的boost :: unit_test :: test_case :: test_case
更新:
我刚刚发现,不仅版本的
G ++
问题在这里,但也编译标志。试想一下:// foo.cc
#包括LT&;串GT;
无效美孚(常量标准::字符串&安培;){}// t.cc
#包括LT&;串GT;
无效美孚(常量标准::字符串&安培;);
INT的main(){性病::字符串s;富(S); }使用GCC近期主干:
G ++ -c t.cc foo.cc
G ++ T.O foo.o的成功#G ++ -D_GLIBCXX_USE_CXX11_ABI = 0 t.cc foo.o的
/tmp/ccKqZVvh.o:在函数'主':
t.cc:(.text+0x1d):未定义的引用`美孚(标准::字符串常量和放大器;)
collect2:错误:LD返回1退出状态I'm stuck trying to figure out why my tests won't link. I'm using Boost.Build and Boost.Test from Boost 1.59 on Ubuntu. I built and installed the boost libraries from source.
I am specifying
<library>/boost//unit_test_framework
as part of the unit test's usage requirements. My understanding is that this should set all of the linker requirements in order to link with Boost.Test.Unfortunately, I get a heap of linker errors for undefined references to Boost.Test components even though the link command is specifying
-lboost_unit_test_framework
after thetest.o
file.I have a very simple example that exhibits the described behaviour:
In
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 ;
In
test.cpp
:#define BOOST_TEST_MODULE test #include <boost/test/unit_test.hpp> BOOST_AUTO_TEST_CASE (pass) { }
Running
b2 -d+2
gives me the compilation and linker commands that are used.$ 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
Note that with the use of
-Wl,--start-group ...test.o -lboost_unit_test_framework -Wl,--end-group
the order of
test.o
and the library doesn't matter: because of--{start,end}-group
, they are considered to be part of the same "bookshelf".It must be that the
libboost_unit_test.{a,so}
does not provide the symbols that you need. Perhaps it was built with a different version ofg++
? (This answer may be relevant.)Your first step should be to figure out the mangled name of the symbol(s) you are missing: add
-Wl,--no-demangle
to your link line.Then you can verify that you are in fact missing the required symbol(s):
nm -A libboost_unit_test.* | grep missing_mangled_symbol
Finally, you'll need to understand why you are missing that symbol. Is there a similar but slightly different symbol defined? This command may give some clues:
nm -AC libboost_unit_test.* | grep 'boost::unit_test::test_case::test_case'
Update:
I just discovered that not only the version of
g++
matters here, but also the compilation flags. Consider:// 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); }
Using recent GCC trunk:
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
这篇关于使用Boost.Build和Boost.Test - 链接错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!