Reputation: 33864
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. The cmake file for my simple example is shown below.
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:
#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:
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:
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. Does anyone know how to link the boost_unit_test in a cmake file?
Upvotes: 29
Views: 25140
Reputation: 1113
For the ones who use CMake's FetchContent
:
in cmake/Boost.cmake:
include(FetchContent)
FetchContent_Declare(
Boost
GIT_REPOSITORY https://github.com/boostorg/boost.git
GIT_TAG boost-1.86.0
GIT_SHALLOW 1
)
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
set(BOOST_ENABLE_CMAKE ON)
set(BOOST_INCLUDE_LIBRARIES
test
)
FetchContent_MakeAvailable(Boost)
Then in the CMakeLists.txt:
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
Then in test/CMakeLists.txt:
include(Boost)
add_executable(boost_test_toh
boost_test_terminal_toh.cpp
)
target_link_libraries(boost_test_toh
PRIVATE precompiled
PRIVATE Boost::unit_test_framework
PRIVATE terminal_toh_static
)
Add whatever other Boost library that you need to BOOST_INCLUDE_LIBRARIES
. Here is the list of available options.
And here is the CMakeProjectTemplate putting all these into action.
Upvotes: 1
Reputation: 366
Nowadays in CMake 3, I'd recommend using the exported namespace targets instead of variables. If you have a typo, Fraser's answer fails to link. Also, specify the linkage type explicitly if you can.
find_package(Boost CONFIG COMPONENTS system filesystem unit_test_framework REQUIRED)
...
target_link_libraries(testTheTester
PRIVATE
Boost::filesystem
Boost::system
Boost::unit_test_framework
)
Upvotes: 4
Reputation: 78310
You need to include the unit test framework in the list of requirements in the find_package
command, and then link it:
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}
)
Upvotes: 24