Reputation: 1508
I'm having trouble building a little program that uses Boost.Test on my Mac with a Boost installed by MacPorts at /opt/local/lib/
Here's my minimal source file, test.cpp
:
#define BOOST_TEST_MODULE MyTest
#include <boost/test/unit_test.hpp>
BOOST_AUTO_TEST_CASE(test1) {
}
and my CMakeLists.txt
:
cmake_minimum_required(VERSION 2.6)
project (test)
find_package(Boost COMPONENTS unit_test_framework REQUIRED)
add_executable(test test.cpp)
and an excerpt of from 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.
As you can see, it doesn't know how to link to Boost library. So I try adding to CMakeLists.txt:
target_link_libraries(test boost_unit_test_framework)
But I just get:
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
From lots of trial and error, I've found that manually running this works:
$ g++ test.cpp -L/opt/local/lib -lboost_unit_test_framework -DBOOST_TEST_DYN_LINK
But after hours of fiddling, I can't get it to build from CMake. I don't care whether it links dynamically or statically, I just want it to work.
Upvotes: 4
Views: 3799
Reputation: 3385
The details of building a Boost.Test module are on the documentation here and covered with many examples on the documentation. Usually if main
is not found, this might be due to:
BOOST_TEST_MODULE
and/or BOOST_TEST_DYN_LINK
macros: depending on those, the Boost.Test framework will define (correctly) a main
or not.Well here, the problem is not that cmake does not find the boost_unit_test_framework
library, but rather that this specific library does not contain the main
entry point for running the binary.
In fact, you should link against ${Boost_TEST_EXEC_MONITOR_LIBRARY}
since it contains the proper definitions. You should also avoid defining the macro BOOST_TEST_DYN_LINK
.
Upvotes: 1
Reputation: 65791
The find_package(Boost COMPONENTS ...)
call collects the required link libraries for the searched Boost components (e.g.,unit_test_framework
) in the CMake variable Boost_LIBRARIES
.
To get rid of the link error, add:
target_link_libraries(test ${Boost_LIBRARIES})
Upvotes: 3
Reputation:
You need to tell CMake where to find the boost libraries (the -L/opt/local/lib
in your g++ line). You can accomplish this by adding the following line (if you had no problem with find_package
):
link_directories ( ${Boost_LIBRARY_DIRS} )
before add_executable
.
Another alternative is using the single-header variant of the UTF. This variant is really simple (you only need to include <boost/test/included/unit_test.hpp>
but it has a major drawback in its considerable increase in build time.
Upvotes: 5