【问题标题】:Boost.Tests where is entry point? [duplicate]Boost.Tests 入口点在哪里? [复制]
【发布时间】:2015-04-30 09:18:33
【问题描述】:

我正在使用 JetBrain 的 CLion 并尝试运行一些增强测试,但它们不会。 这是我的代码:

#define BOOST_TEST_MAIN 1
#define BOOST_TEST_MODULE !
#include <boost/test/unit_test.hpp>
#include <iostream>

BOOST_AUTO_TEST_CASE(MyTest) {
    BOOST_CHECK(false);
}


int main() {
    std::cout << "in main!" << std::endl;
    return 0;
}

还有我的 cmake 文件:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
set(BOOST_ROOT /home/vitalii/Downloads/boost_1_57_0)
find_package(Boost 1.57 COMPONENTS unit_test_framework REQUIRED)
add_executable(justForFun ${SOURCE_FILES})
if (Boost_FOUND)
    include_directories(${Boost_INCLUDE_DIRS})
    link_directories(${Boost_LIBRARY_DIRS})
    target_link_libraries(justForFun ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY})
else()
    message(FATAL "Boost not found")
endif()
enable_testing()

问题是输出将只是“主要”。并且测试不会启动。我想我不需要“主要”功能。它应该是自动创建的。但是如果我删除它,我会收到这些错误:

/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

有人可以帮帮我吗?

【问题讨论】:

  • Boost 链接是否正确(参见this)?或者this 对你有帮助吗?
  • @TobiMcNamobi 谢谢,确实如此。我在第二个链接中添加了ADD_DEFINITIONS(-DBOOST_TEST_DYN_LINK),它起作用了。如果我写#include &lt;boost/test/included/unit_test.hpp&gt; insted of #include &lt;boost/test/unit_test.hpp&gt;,它也可以工作。你能解释一下为什么吗?如果您将评论发布为答案,我将标记为已接受。谢谢!
  • 搜索“boost/test/included”提供this(以及其他)。

标签: c++ boost cmake clion boost.test


【解决方案1】:

你写的

#define BOOST_TEST_MODULE !

在我发现的示例中,BOOST_TEST_MODULE 被定义为一些有效的标识符,例如

#define BOOST_TEST_MODULE MyModule

这也适用于我的项目。如果您改为定义自己的 main() 函数,我猜想根本没有使用 BOOST_TEST_MODULE 这解释了您的结果。

更多参考资料见here

【讨论】:

  • 感谢您的回复。但是我将 BOOST_TEST_MODULE 更改为 MyModule 并且没有定义 main() 并且仍然得到“未定义的对 `main' 的引用”
猜你喜欢
  • 1970-01-01
  • 2012-03-03
  • 2022-01-06
  • 2013-04-18
  • 1970-01-01
  • 2020-02-26
  • 2020-05-12
  • 2018-08-02
  • 2020-12-03
相关资源
最近更新 更多