增强单元测试入口点问题

时间:2014-07-03 15:57:27

标签: c++ eclipse unit-testing boost

以下是我在Eclipse CDT中使用的代码(来自官方提升文档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测试,所以它应该是即插即用减去我指定库(-l)的部分为boost_unit_test_framework。

我已经能够获得boost / test / minimal.hpp链接并且工作正常,但不幸的是我还没有得到boost / test / unit_test.hpp在Eclipse中很好地运行单元测试输出显示在C / C ++单元视图中。我尝试过几个官方和非正式的文档/教程,并且还没有成功。我试图在Eclipse中开始进行单元测试,这与在Eclipse中使用JUnit的舒适性有些相似。任何帮助或建议表示赞赏。感谢。

1 个答案:

答案 0 :(得分:0)

想出来。事实证明我应该避免通过包管理器安装框架库。最好从源代码安装,以便了解所有内容。这两个文档有所帮助:

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

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

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

我不确定软件包管理器放置库的位置,但我需要在链接时和运行时验证链接的完整性,确保将库文件放在/ usr / local / lib中然后跟进ldconfig。