可能重复:
为什么 gcc 中“-l”选项的顺序很重要?
我开始学习 Boost 单元测试框架。我有一个最小的测试套件:
#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
但是,如果我交换and的顺序-lboost_unit_test_framework
,我会得到链接器错误:
g++ -o tests -lboost_unit_test_framework src/tests.o
src/tests.o:在函数“main”中: tests.cc:(.text+0x29): undefined reference to `boost::unit_test::unit_test_main(bool (*)(), int, char**)' src/tests.o:在函数“test1::test_method()”中: tests.cc:(.text+0x9d): 未定义引用`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:在函数“__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:在函数`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: 在函数`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): 未定义引用`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): 未定义引用`boost::unit_test test_case::test_case(boost::unit_test::basic_cstring, boost::unit_test::callback0 const&)' src/tests.o:在函数`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 返回 1 个退出状态
为什么我的参数顺序会导致链接器错误?