简体   繁体   English

用Cmake编译一个boost测试

[英]Compiling a boost test with Cmake

I am trying to simplify a large project by having cmake compile it all for me, but i am having trouble compiling the boost unit tests. 我正在尝试通过让cmake为我编译所有内容来简化大型项目,但是我在编译boost单元测试时遇到了问题。 The cmake file for my simple example is shown below. 我的简单示例的cmake文件如下所示。

cmake_minimum_required(VERSION 2.8)
find_package(Boost COMPONENTS system filesystem REQUIRED)
add_excecutable(testTheTester boostTester.cpp)
target_link_libraries(testTheTester ${Boost_FILESYSTEM_LIBRARY} ${Boost_SYSTEM_LIBRARY})
add_test(tester tester)

and the code in boostTester.cpp is: 而boostTester.cpp中的代码是:

#define BOOST_TEST_MAIN
#if !defined( WIN32 )
    #define BOOST_TEST_DYN_LINK
#endif
#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_CASE( la ) {
    BOOST_CHECK_EQUAL(1, 1)
}

Now this cpp code will compile and run fine if i build it manually with: 现在这个cpp代码将编译并运行正常,如果我手动构建它:

g++ boostTester.cpp -o output -lboost_unit_test_framework

and the cmake works fine but when using the output make file the make crashes with a huge amount of errors the first of which is this: 并且cmake工作正常,但是当使用输出make文件时,make崩溃时会出现大量错误,其中第一个是:

undefined referance to 'boost::unit_test::unit_test_log_t::set_checkpoint(boost... bla bla

now my initial thought is that the cmake is not linking the boost library correctly and I have tried many commands and combinations with no luck. 现在我最初的想法是cmake没有正确链接boost库,我尝试了许多命令和组合,没有运气。 Does anyone know how to link the boost_unit_test in a cmake file? 有谁知道如何在cmake文件中链接boost_unit_test?

You need to include the unit test framework in the list of requirements in the find_package command, and then link it: 您需要在find_package命令的需求列表中包含单元测试框架,然后将其链接:

find_package(Boost COMPONENTS system filesystem unit_test_framework REQUIRED)
...
target_link_libraries(testTheTester
                      ${Boost_FILESYSTEM_LIBRARY}
                      ${Boost_SYSTEM_LIBRARY}
                      ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}
                      )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM