簡體   English   中英

使用CMake和Boost單元測試進行編譯

[英]Compiling with CMake and Boost unit tests

我是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:

曾嘗試進行編輯和升級以增強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.

您必須正確鏈接以增強功能,而不是添加包含目錄標志。

在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
)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM