0

I'm relatively new to C++, and I'm trying to compile a project using CMake with the following #include:

#include <boost/test/unit_test.hpp>

I get the following error

undefined symbols for architecture x86_64:
"boost::unit_test::unit_test_log_t::instance()", referenced from:
___cxx_global_var_init in functions1.cpp.o
___cxx_global_var_init in functions2.cpp.o
___cxx_global_var_init in main.cpp.o
ld: symbol(s) not found for architecture x86_64

I'm pretty sure this is something to do with my CMakeLists.txt, so here it is:

cmake_minimum_required(VERSION 3.13)
project(MyProject)

set(CMAKE_CXX_STANDARD 14)

include_directories(.)
include_directories(/usr/local/include/)

add_executable(MyProject
    functions1.cpp
    functions1.h
    functions2.cpp
    functions2.h
    main.cpp
    main.h
    utillity.cpp
    utillity.h)

set(BOOST_ROOT "/usr/local/Cellar/boost/1.69.0_2")

find_package(Boost COMPONENTS filesystem system unit_test_framework REQUIRED)

#find_package(Boost 1.69.0)

if(NOT Boost_FOUND)
    message(FATAL_ERROR "Could not find boost!")
endif()

include_directories (${Boost_INCLUDE_DIRS})

I'm on OSX Mojave, and installing with brew install boost. I'm aware there are several posts out there that report very similar issues, but none of the solutions seem to work for me.

Edit:

Have adjusted my CMakeLists to the following based on Guillaume's suggestion below.

make_minimum_required(VERSION 3.13)
project(MyProject)

set(CMAKE_CXX_STANDARD 14)

add_executable(MyProject
    functions1.cpp
    functions1.h
    functions2.cpp
    functions2.h
    main.cpp
    main.h
    utillity.cpp
    utillity.h)

set(BOOST_ROOT "/usr/local/Cellar/boost/1.69.0_2")

find_package(Boost COMPONENTS filesystem system test REQUIRED)

target_include_directories(MyProject PUBLIC ".")

target_link_libraries(MyProject PUBLIC
    Boost::filesystem Boost::system Boost::test)

I understand that this is, in principle, better, but it's giving me:

Unable to find the requested Boost libraries.

Boost version: 1.69.0

Boost include path: /usr/local/Cellar/boost/1.69.0_2/include

Could not find the following Boost libraries:

    boost_test

Some (but not all) of the required Boost libraries were found.  You may need to install these additional Boost libraries.  Alternatively, set BOOST_LIBRARYDIR to the directory containing Boost libraries or BOOST_ROOT to the location of Boost.

Edit 2:

Have tried edits and upgrading to boost 1.7, but still have:

Could not find a package configuration file provided by "boost_test"
(requested version 1.70.0) with any of the following names:

boost_testConfig.cmake
boost_test-config.cmake

Add the installation prefix of "boost_test" to CMAKE_PREFIX_PATH or set
"boost_test_DIR" to a directory containing one of the above files.  If
"boost_test" provides a separate development package or SDK, be sure it 
has been installed.
C.Marsden
  • 43
  • 6
  • Can this help? https://stackoverflow.com/questions/9746065/boost-test-with-cmake-undefined-main – Renat May 14 '19 at 16:17
  • You use `find_package(Boost)` but among its results you use only include directories (via variable `Boost_INCLUDE_DIRS`). You need to **link** with the libraries as well. (And "undefined symbols" signals about missed linking with a libraries). – Tsyvarev May 14 '19 at 16:18

1 Answers1

1

You have to link properly to boost instead of adding include directory flags.

Linking libraries in cmake will apply all required property to use boost onto your targets.

First off, don't do that:

include_directories(.)
include_directories(/usr/local/include/)

This is asking for link error. It will enable you to use headers of libraries you don't link properly to. This will cause linking errors, even within your own project.

cmake_minimum_required(VERSION 3.13)
project(MyProject)

set(CMAKE_CXX_STANDARD 14)

add_executable(MyProject
    functions1.cpp
    functions1.h
    functions2.cpp
    functions2.h
    main.cpp
    main.h
    utillity.cpp
    utillity.h)

list(APPEND CMAKE_PREFIX_PATH "/usr/local/Cellar/boost/1.69.0_2")
set(Boost_ADDITIONAL_VERSIONS "1.69.0" "1.69")
find_package(Boost COMPONENTS filesystem system test REQUIRED)

# Not needed
#if(NOT Boost_FOUND)
#    message(FATAL_ERROR "Could not find boost!")
#endif()

# include_directories (${Boost_INCLUDE_DIRS})

target_include_directories(MyProject PUBLIC ".")

# adds include directories, definitions and link libraries
target_link_libraries(MyProject PUBLIC
    Boost::filesystem Boost::system Boost::test
)
Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141