Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost Test with CMake - undefined main

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.

like image 963
exclipy Avatar asked Oct 24 '25 04:10

exclipy


2 Answers

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.

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})
like image 21
sakra Avatar answered Oct 26 '25 22:10

sakra



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!