当我改变g ++参数的顺序时,为什么我的程序无法链接?

时间:2012-08-12 23:12:35

标签: c++ boost compilation linker g++

  

可能重复:
  Why does the order of '-l' option in gcc matter?

我开始学习Boost Unit Test框架。我有一个最小的测试套件:

#define BOOST_TEST_MAIN
#define BOOST_TEST_DYN_LINK 
#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_CASE( test1 ) {
    BOOST_CHECK( 2 == 1 );
}

首先我编译源代码:

g++ -c src/tests.cc -o src/tests.o

这样就没有错误了。然后我可以按如下方式链接:

g++ -o tests src/tests.o -lboost_unit_test_framework

这也完成没有错误。生成的二进制文件执行预期结果。但是,如果我交换src/tests.o-lboost_unit_test_framework的顺序,我会收到链接器错误:

g++ -o tests -lboost_unit_test_framework src/tests.o
src/tests.o: In function `main':
tests.cc:(.text+0x29): undefined reference to `boost::unit_test::unit_test_main(bool (*)(), int, char**)'
src/tests.o: In function `test1::test_method()':
tests.cc:(.text+0x9d): undefined reference to `boost::unit_test::unit_test_log_t::set_checkpoint(boost::unit_test::basic_cstring, unsigned int, boost::unit_test::basic_cstring)'
tests.cc:(.text+0x146): undefined reference to `boost::test_tools::tt_detail::check_impl(boost::test_tools::predicate_result const&, boost::unit_test::lazy_ostream const&, boost::unit_test::basic_cstring, unsigned int, boost::test_tools::tt_detail::tool_level, boost::test_tools::tt_detail::check_type, unsigned int, ...)'
src/tests.o: In function `__static_initialization_and_destruction_0(int, int)':
tests.cc:(.text+0x24d): undefined reference to `boost::unit_test::ut_detail::auto_test_unit_registrar::auto_test_unit_registrar(boost::unit_test::test_case*, unsigned long)'
src/tests.o: In function `boost::unit_test::unit_test_log_t::unit_test_log_t()':
tests.cc:(.text._ZN5boost9unit_test15unit_test_log_tC2Ev[_ZN5boost9unit_test15unit_test_log_tC5Ev]+0x21): undefined reference to `vtable for boost::unit_test::unit_test_log_t'
src/tests.o: In function `boost::unit_test::make_test_case(boost::unit_test::callback0 const&, boost::unit_test::basic_cstring)':
tests.cc:(.text._ZN5boost9unit_test14make_test_caseERKNS0_9callback0INS0_9ut_detail6unusedEEENS0_13basic_cstringIKcEE[boost::unit_test::make_test_case(boost::unit_test::callback0 const&, boost::unit_test::basic_cstring)]+0x1d): undefined reference to `boost::unit_test::ut_detail::normalize_test_case_name(boost::unit_test::basic_cstring)'
tests.cc:(.text._ZN5boost9unit_test14make_test_caseERKNS0_9callback0INS0_9ut_detail6unusedEEENS0_13basic_cstringIKcEE[boost::unit_test::make_test_case(boost::unit_test::callback0 const&, boost::unit_test::basic_cstring)]+0x5d): undefined reference to `boost::unit_test::test_case::test_case(boost::unit_test::basic_cstring, boost::unit_test::callback0 const&)'
src/tests.o: In function `boost::unit_test::unit_test_log_t::~unit_test_log_t()':
tests.cc:(.text._ZN5boost9unit_test15unit_test_log_tD2Ev[_ZN5boost9unit_test15unit_test_log_tD5Ev]+0xb): undefined reference to `vtable for boost::unit_test::unit_test_log_t'
collect2: ld returned 1 exit status

为什么我的参数的顺序会导致链接器错误?

2 个答案:

答案 0 :(得分:11)

当GCC执行链接时,会对库进行特殊处理:只有在命令行列表中的库之前来自的对象文件中缺少的符号才会从库中填充。如果在库之后还有其他目标文件,则不会在库中查找该对象中缺少的符号。

简而言之,首先列出目标文件,最后列出库。

答案 1 :(得分:3)

  

链接器的传统行为是在命令行中指定的库中从左到右搜索外部函数。这意味着包含函数定义的库应该出现在使用它的任何源文件或目标文件之后。这包括使用short-cut -l选项指定的库,如以下命令所示:

http://www.network-theory.co.uk/docs/gccintro/gccintro_18.html