【问题标题】:Compiling with CMake and Boost unit tests使用 CMake 和 Boost 单元测试进行编译
【发布时间】:2019-05-14 16:14:42
【问题描述】:

我对 C++ 比较陌生,我正在尝试使用带有以下 #include 的 CMake 编译项目:

#include <boost/test/unit_test.hpp>

我收到以下错误

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

我很确定这与我的 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})

我在 OSX Mojave 上,并使用 brew install boost 进行安装。我知道有几篇帖子报告了非常相似的问题,但似乎没有一个解决方案适合我。

编辑:

根据 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)

我知道这原则上更好,但它给了我:

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.

编辑 2:

已尝试编辑和升级到 boost 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.

【问题讨论】:

  • 您使用find_package(Boost),但在其结果中您仅使用包含目录(通过变量Boost_INCLUDE_DIRS)。您还需要与库链接。 (以及“未定义符号”表示错过与库的链接)。

标签: c++ boost cmake


【解决方案1】:

您必须正确链接才能提升而不是添加包含目录标志。

cmake 中的链接库将应用所有必需的属性以在您的目标上使用 boost。

首先,不要那样做:

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

这是要求链接错误。它将使您能够使用未正确链接到的库的标题。这导致链接错误,即使在您自己的项目中也是如此。

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
)

【讨论】:

  • 已更新原始帖子以反映这一点。这现在给了我一个“无法找到 Boost Library”的错误。
  • @C.Marsden 您是否按照 Boost 构建说明进行操作?
  • @C.Marsden 试试我的编辑。请记住,boost 1.70 具有更好的 cmake 集成。
  • 遗憾的是没有变化,但我将研究 1.7 版
猜你喜欢
  • 2012-04-10
  • 1970-01-01
  • 2012-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多