【问题标题】:Boost Unit Test Entry Point IssueBoost 单元测试入口点问题
【发布时间】:2014-08-24 20:30:43
【问题描述】:

这是我在 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 函数会使程序构建良好,但随后会完全跳过测试。我使用突触包管理器安装了boost测试,所以它应该几乎是即插即用的,减去我将库(-l)指定为boost_unit_test_framework的部分。

我已经能够获得 boost/test/minimal.hpp 链接并且工作得很好,但不幸的是我没有让 boost/test/unit_test.hpp 在 Eclipse 中很好地工作,其中单元测试输出显示在C/C++ 单元视图。我已经尝试过几个官方和非官方的 boost 文档/教程,但都不太成功。我试图达到可以在 Eclipse 中开始单元测试的程度,这有点类似于在 Eclipse 中使用 JUnit 的舒适性。任何帮助或建议表示赞赏。谢谢。

【问题讨论】:

    标签: c++ eclipse unit-testing boost


    【解决方案1】:

    想通了。原来我应该避免通过包管理器安装框架库。最好从源代码安装,这样您就知道所有内容在哪里。这两个文档有所帮助:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-30
      • 1970-01-01
      • 2016-08-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多