提升单元测试入口点问题

Boost Unit Test Entry Point Issue

本文关键字:问题 入口 单元测试      更新时间:2023-10-16

下面是我在Eclipse CDT中使用的代码(来自官方boost文档http://www.boost.org/doc/libs/1_55_0/libs/test/doc/html/tutorials/hello-the-testing-world.html):

#define BOOST_TEST_MODULE MyTest
#include <boost/test/unit_test.hpp>
int add(int i, int j) {
   return i + j;
}
BOOST_AUTO_TEST_CASE( my_test ) {
   // seven ways to detect and report the same error:
   BOOST_CHECK(add(2, 2) == 4);        // #1 continues on error
   BOOST_REQUIRE(add(2, 2) == 4);      // #2 throws on error
   if (add(2, 2) != 4)
      BOOST_ERROR("Ouch...");            // #3 continues on error
   if (add(2, 2) != 4)
      BOOST_FAIL("Ouch...");             // #4 throws on error
   if (add(2, 2) != 4)
      throw "Ouch..."; // #5 throws on error
   BOOST_CHECK_MESSAGE(add(2, 2) == 4,  // #6 continues on error
   "add(..) result: " << add( 2,2 ));
   BOOST_CHECK_EQUAL(add(2, 2), 4);      // #7 continues on error
}

当我尝试在Eclipse或终端中构建它时,我得到一条关于缺乏入口点或主函数的错误消息:

08:33:51 **** Incremental Build of configuration Debug for project Test_C++ ****
make all 
Building target: Test_C++
Invoking: GCC C++ Linker
g++  -o "Test_C++"  ./src/test.o   -lboost_unit_test_framework
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 2 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 3 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 4 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 5 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 6 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 7 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 8 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 9 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 10 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 11 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 12 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 13 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 14 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 15 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 16 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 17 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 18 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 19 has invalid symbol index 21
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_line): relocation 0 has invalid symbol index 2
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
make: *** [Test_C++] Error 1
08:33:52 Build Finished (took 742ms)

在代码中加入main函数可以使程序正常构建,但随后会完全跳过测试。我使用synaptic包管理器安装boost test,所以它应该几乎是即插即用的,减去我将库(-l)指定为boost_unit_test_framework的部分。

我已经能够得到boost/test/minimum .hpp链接和工作很好,但不幸的是,我没有得到boost/test/unit_test.hpp在Eclipse中很好地工作,其中单元测试输出显示在C/c++单元视图中。我试着浏览了几个官方和非官方的boost文档/教程,但都不太成功。我正试图达到一个可以在Eclipse中开始单元测试的点,它在某种程度上类似于在Eclipse中使用JUnit的舒适性。任何帮助或建议都是感激的。谢谢。

明白了。原来我应该避免通过包管理器安装框架库。最好从源代码安装,这样您就知道所有东西的位置。这两个文档有帮助:

http://ubuntuforums.org/showthread.php?t=1180792

对于处理共享库的一般情况(即在bjam安装阶段之后):

http://www.cprogramming.com/tutorial/shared-libraries-linux-gcc.html

我不太确定包管理器把库放在哪里,但我需要在链接时和运行时验证链接的完整性,确保我把库文件放在/usr/local/lib中,然后再使用ldconfig。