【发布时间】: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)。您还需要与库链接。 (以及“未定义符号”表示错过与库的链接)。