这个标题已经提出了许多问题,但我发现没有任何答案可供我使用......
我有以下代码:
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE Hello
#include <boost/test/unit_test.hpp>
int add(int i, int j)
{
return i + j;
}
BOOST_AUTO_TEST_CASE(universeInOrder)
{
BOOST_CHECK(add(2, 2) == 4);
}
我编译:
g++ -ohello -lboost_unit_test_framework hello.cpp
我得到的错误:
g++ -ohello -lboost_unit_test_framework hello.cpp
/usr/bin/ld: error: cannot find -lboost_unit_test_framework
/tmp/ccI94F8l.o:hello.cpp:function init_unit_test(): error: undefined reference to 'boost::unit_test::framework::master_test_suite()'
/tmp/ccI94F8l.o:hello.cpp:function main: error: undefined reference to 'boost::unit_test::unit_test_main(bool (*)(), int, char**)'
/tmp/ccI94F8l.o:hello.cpp:function universeInOrder::test_method(): error: undefined reference to 'boost::unit_test::unit_test_log_t::set_checkpoint(boost::unit_test::basic_cstring<char const>, unsigned long, boost::unit_test::basic_cstring<char const>)'
/tmp/ccI94F8l.o:hello.cpp:function universeInOrder::test_method(): error: 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<char const>, unsigned long, boost::test_tools::tt_detail::tool_level, boost::test_tools::tt_detail::check_type, unsigned long, ...)'
/tmp/ccI94F8l.o:hello.cpp:function __static_initialization_and_destruction_0(int, int): error: undefined reference to 'boost::unit_test::ut_detail::auto_test_unit_registrar::auto_test_unit_registrar(boost::unit_test::test_case*, unsigned long)'
/tmp/ccI94F8l.o:hello.cpp:function boost::unit_test::unit_test_log_t::unit_test_log_t(): error: undefined reference to 'vtable for boost::unit_test::unit_test_log_t'
/tmp/ccI94F8l.o:hello.cpp:function boost::unit_test::make_test_case(boost::unit_test::callback0<boost::unit_test::ut_detail::unused> const&, boost::unit_test::basic_cstring<char const>): error: undefined reference to 'boost::unit_test::ut_detail::normalize_test_case_name(boost::unit_test::basic_cstring<char const>)'
/tmp/ccI94F8l.o:hello.cpp:function boost::unit_test::make_test_case(boost::unit_test::callback0<boost::unit_test::ut_detail::unused> const&, boost::unit_test::basic_cstring<char const>): error: undefined reference to 'boost::unit_test::test_case::test_case(boost::unit_test::basic_cstring<char const>, boost::unit_test::callback0<boost::unit_test::ut_detail::unused> const&)'
/tmp/ccI94F8l.o:hello.cpp:function boost::unit_test::unit_test_log_t::~unit_test_log_t(): error: undefined reference to 'vtable for boost::unit_test::unit_test_log_t'
collect2: ld returned 1 exit status
简单的例子来自http://www.alittlemadness.com/2009/03/31/c-unit-testing-with-boosttest/我也遇到了另一个大项目的问题,在其他机器上工作正常......我的机器配置一定有问题,但我不知道是什么(或者如何调试它!)。
我已经做过的一些事情:
检查libc.conf:
$ cat /etc/ld.so.conf.d/libc.conf
# libc default configuration
/usr/local/lib
检查是否可以以用户身份读取增强文件:
$ test -r /usr/local/lib/libboost_unit_test_framework.a && echo ok
ok
$ ll /usr/local/lib/libboost_program_options.a
-rw-r--r-- 1 root root 1.2M May 22 2013 /usr/local/lib/libboost_program_options.a
运行ldconfig?
sudo ldconfig
g ++ -Wl, - verbose -o hello hello.cpp -lboost_unit_test_framework
/usr/bin/ld: Attempt to open /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o succeeded
/usr/bin/ld: Attempt to open /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crti.o succeeded
/usr/bin/ld: Attempt to open /usr/lib/gcc/x86_64-linux-gnu/4.6/crtbegin.o succeeded
/usr/bin/ld: Attempt to open /tmp/ccOe343V.o succeeded
/usr/bin/ld: Attempt to open /usr/lib/gcc/x86_64-linux-gnu/4.6/crtend.o succeeded
/usr/bin/ld: Attempt to open /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crtn.o succeeded
/usr/bin/ld: Attempt to open /usr/lib/gcc/x86_64-linux-gnu/4.6/libboost_unit_test_framework.so failed
/usr/bin/ld: Attempt to open /usr/lib/gcc/x86_64-linux-gnu/4.6/libboost_unit_test_framework.a failed
/usr/bin/ld: Attempt to open /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/libboost_unit_test_framework.so failed
/usr/bin/ld: Attempt to open /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/libboost_unit_test_framework.a failed
/usr/bin/ld: Attempt to open /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../../lib/libboost_unit_test_framework.so failed
/usr/bin/ld: Attempt to open /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../../lib/libboost_unit_test_framework.a failed
/usr/bin/ld: Attempt to open /lib/x86_64-linux-gnu/libboost_unit_test_framework.so failed
/usr/bin/ld: Attempt to open /lib/x86_64-linux-gnu/libboost_unit_test_framework.a failed
/usr/bin/ld: Attempt to open /lib/../lib/libboost_unit_test_framework.so failed
/usr/bin/ld: Attempt to open /lib/../lib/libboost_unit_test_framework.a failed
/usr/bin/ld: Attempt to open /usr/lib/x86_64-linux-gnu/libboost_unit_test_framework.so failed
/usr/bin/ld: Attempt to open /usr/lib/x86_64-linux-gnu/libboost_unit_test_framework.a failed
/usr/bin/ld: Attempt to open /usr/lib/../lib/libboost_unit_test_framework.so failed
/usr/bin/ld: Attempt to open /usr/lib/../lib/libboost_unit_test_framework.a failed
/usr/bin/ld: Attempt to open /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../libboost_unit_test_framework.so failed
/usr/bin/ld: Attempt to open /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../libboost_unit_test_framework.a failed
/usr/bin/ld: Attempt to open //lib/x86_64-linux-gnu/libboost_unit_test_framework.so failed
/usr/bin/ld: Attempt to open //lib/x86_64-linux-gnu/libboost_unit_test_framework.a failed
/usr/bin/ld: Attempt to open //usr/lib/x86_64-linux-gnu/libboost_unit_test_framework.so failed
/usr/bin/ld: Attempt to open //usr/lib/x86_64-linux-gnu/libboost_unit_test_framework.a failed
/usr/bin/ld: Attempt to open //lib/libboost_unit_test_framework.so failed
/usr/bin/ld: Attempt to open //lib/libboost_unit_test_framework.a failed
/usr/bin/ld: Attempt to open //usr/lib/libboost_unit_test_framework.so failed
/usr/bin/ld: Attempt to open //usr/lib/libboost_unit_test_framework.a failed
/usr/bin/ld: error: cannot find -lboost_unit_test_framework
/usr/bin/ld: Attempt to open /usr/lib/gcc/x86_64-linux-gnu/4.6/libstdc++.so succeeded
/usr/bin/ld: Attempt to open /usr/lib/gcc/x86_64-linux-gnu/4.6/libm.so failed
/usr/bin/ld: Attempt to open /usr/lib/gcc/x86_64-linux-gnu/4.6/libm.a failed
/usr/bin/ld: Attempt to open /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/libm.so succeeded
/usr/bin/ld: Attempt to open /usr/lib/gcc/x86_64-linux-gnu/4.6/libgcc_s.so succeeded
/usr/bin/ld: Attempt to open /usr/lib/gcc/x86_64-linux-gnu/4.6/libgcc.so failed
/usr/bin/ld: Attempt to open /usr/lib/gcc/x86_64-linux-gnu/4.6/libgcc.a succeeded
/usr/bin/ld: Attempt to open /usr/lib/gcc/x86_64-linux-gnu/4.6/libc.so failed
/usr/bin/ld: Attempt to open /usr/lib/gcc/x86_64-linux-gnu/4.6/libc.a failed
/usr/bin/ld: Attempt to open /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/libc.so succeeded
/usr/bin/ld: Attempt to open /lib/x86_64-linux-gnu/libc.so.6 succeeded
/usr/bin/ld: Attempt to open /usr/lib/x86_64-linux-gnu/libc_nonshared.a succeeded
/usr/bin/ld: Attempt to open /lib/x86_64-linux-gnu/ld-linux-x86-64.so.2 succeeded
/usr/bin/ld: Attempt to open /usr/lib/gcc/x86_64-linux-gnu/4.6/libgcc_s.so succeeded
/usr/bin/ld: Attempt to open /usr/lib/gcc/x86_64-linux-gnu/4.6/libgcc.so failed
/usr/bin/ld: Attempt to open /usr/lib/gcc/x86_64-linux-gnu/4.6/libgcc.a succeeded
/tmp/ccOe343V.o:hello.cpp:function init_unit_test(): error: undefined reference to 'boost::unit_test::framework::master_test_suite()'
/tmp/ccOe343V.o:hello.cpp:function main: error: undefined reference to 'boost::unit_test::unit_test_main(bool (*)(), int, char**)'
/tmp/ccOe343V.o:hello.cpp:function universeInOrder::test_method(): error: undefined reference to 'boost::unit_test::unit_test_log_t::set_checkpoint(boost::unit_test::basic_cstring<char const>, unsigned long, boost::unit_test::basic_cstring<char const>)'
/tmp/ccOe343V.o:hello.cpp:function universeInOrder::test_method(): error: 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<char const>, unsigned long, boost::test_tools::tt_detail::tool_level, boost::test_tools::tt_detail::check_type, unsigned long, ...)'
/tmp/ccOe343V.o:hello.cpp:function __static_initialization_and_destruction_0(int, int): error: undefined reference to 'boost::unit_test::ut_detail::auto_test_unit_registrar::auto_test_unit_registrar(boost::unit_test::test_case*, unsigned long)'
/tmp/ccOe343V.o:hello.cpp:function boost::unit_test::unit_test_log_t::unit_test_log_t(): error: undefined reference to 'vtable for boost::unit_test::unit_test_log_t'
/tmp/ccOe343V.o:hello.cpp:function boost::unit_test::make_test_case(boost::unit_test::callback0<boost::unit_test::ut_detail::unused> const&, boost::unit_test::basic_cstring<char const>): error: undefined reference to 'boost::unit_test::ut_detail::normalize_test_case_name(boost::unit_test::basic_cstring<char const>)'
/tmp/ccOe343V.o:hello.cpp:function boost::unit_test::make_test_case(boost::unit_test::callback0<boost::unit_test::ut_detail::unused> const&, boost::unit_test::basic_cstring<char const>): error: undefined reference to 'boost::unit_test::test_case::test_case(boost::unit_test::basic_cstring<char const>, boost::unit_test::callback0<boost::unit_test::ut_detail::unused> const&)'
/tmp/ccOe343V.o:hello.cpp:function boost::unit_test::unit_test_log_t::~unit_test_log_t(): error: undefined reference to 'vtable for boost::unit_test::unit_test_log_t'
collect2: ld returned 1 exit status
答案 0 :(得分:3)
我的机器配置肯定有问题,但我不知道是什么(或如何调试它!)
尝试,
g++ -Wl,--verbose -o hello hello.cpp -lboost_unit_test_framework
并查找以下行:尝试打开... boost_unit_test_framework 。它将使您更好地了解链接器用于尝试定位相关静态或动态库的搜索路径。
答案 1 :(得分:2)
我不知道您的确切设置(例如env变量,是否存在共享/静态库等等),所以我提供了各种我认为可行的选项。希望这可以解决你的问题,让我知道所有的失败。
运行$ export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
。这指定使用/usr/local/bin
,这通常被忽略。
运行$ g++ -ohello /usr/local/lib/libboost_unit_test_framework.a hello.cpp
。这指定了确切的库位置。
运行$ g++ -ohello -Wl,-Bstatic -lboost_unit_test_framework hello.cpp
。这指定使用静态库,而不是动态库。
指定使用静态库。我认为提升已经#define了。