加速测试与CMake的 - 未定义主
我无法建立,在/opt/local/lib/
加速测试与CMake的 - 未定义主
这是我最小的源文件,test.cpp
我的Mac上使用如Boost.Test与MacPorts的安装升压一个小程序:
#define BOOST_TEST_MODULE MyTest
#include <boost/test/unit_test.hpp>
BOOST_AUTO_TEST_CASE(test1) {
}
和我CMakeLists.txt
:
cmake_minimum_required(VERSION 2.6)
project (test)
find_package(Boost COMPONENTS unit_test_framework REQUIRED)
add_executable(test test.cpp)
,并从make VERBOSE=1
的摘录:
[100%] Building CXX object CMakeFiles/test.dir/test.cpp.o
g++ -o CMakeFiles/test.dir/test.cpp.o -c /Users/exclipy/Code/cpp/inline_variant/question/test.cpp
Linking CXX executable test
"/Applications/CMake 2.8-5.app/Contents/bin/cmake" -E cmake_link_script CMakeFiles/test.dir/link.txt --verbose=1
g++ -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/test.dir/test.cpp.o-o test
Undefined symbols for architecture x86_64:
"_main", referenced from:
start in crt1.10.6.o
"vtable for boost::unit_test::unit_test_log_t", referenced from:
boost::unit_test::unit_test_log_t::unit_test_log_t() in test.cpp.o
boost::unit_test::unit_test_log_t::~unit_test_log_t() in test.cpp.o
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
正如您所看到的,它不知道如何链接到Boost库。所以我尽量增加的CMakeLists.txt:
target_link_libraries(test boost_unit_test_framework)
但我只是得到:
g++ -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/test.dir/test.cpp.o-o test -lboost_unit_test_framework
ld: library not found for -lboost_unit_test_framework
从大量的试验和错误,我发现手动运行这个工程:
$ g++ test.cpp -L/opt/local/lib -lboost_unit_test_framework -DBOOST_TEST_DYN_LINK
但是经过几小时的摆弄之后,我无法从CMake中构建它。我不在乎它是动态还是静态链接,我只是想让它工作。
您需要告诉CMake在哪里可以找到boost库(您的g ++行中的-L/opt/local/lib
)。您可以通过添加以下行做到这一点(如果您有没有问题,find_package
):
link_directories (${Boost_LIBRARY_DIRS})
之前add_executable
。
另一种选择是使用the single-header variant of the UTF。这种变体是非常简单的(你只需要包括<boost/test/included/unit_test.hpp>
但它在构建时其显着增加的一大缺点。
的find_package(Boost COMPONENTS ...)
调用收集所需的链接库搜索升压组件(例如,unit_test_framework
)在。CMake的变量Boost_LIBRARIES
为了摆脱链接错误,请添加:
target_link_libraries(test ${Boost_LIBRARIES})
那么这里的问题不在于CMake的没有找到boost_unit_test_framework
库,而这个特定的库不包含运行二进制文件的入口点为main
。
事实上,你应该链接到${Boost_TEST_EXEC_MONITOR_LIBRARY}
,因为它包含正确的定义。您还应该避免定义宏BOOST_TEST_DYN_LINK
。
请注意,对于cmake版本> 3.x,(至少)FindBoost.cmake中不存在'Boost_TEST_EXEC_MONITOR_LIBRARY'。 – 2015-08-04 20:02:03