简体   繁体   English

使用CMake和Boost单元测试进行编译

[英]Compiling with CMake and Boost unit tests

I'm relatively new to C++, and I'm trying to compile a project using CMake with the following #include: 我是C ++的新手,我正在尝试使用带有以下#include的CMake编译项目:

#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: 我很确定这与我的CMakeLists.txt有关,所以这里是:

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 . 我在OSX Mojave上,并通过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. 根据以下Guillaume的建议,将我的CMakeLists调整为以下内容。

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: 编辑2:

Have tried edits and upgrading to boost 1.7, but still have: 曾尝试进行编辑和升级以增强1.7,但仍具有:

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.

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. 在cmake中链接库将应用所有必需的属性,以将boost用于目标。

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
)

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

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