c++ - CMake:对 boost 库的 undefined reference

标签 c++ boost cmake

我通过这个添加了 boost :

set(Boost_USE_STATIC_LIBS        ON) 
set(Boost_USE_MULTITHREADED      ON)
set(Boost_USE_STATIC_RUNTIME    OFF)
find_package(Boost REQUIRED)
include_directories(${Boost_INCLUDE_DIR})

project(APP C CXX)
add_executable(APP src.cpp)
target_link_libraries(APP ${Boost_LIBRARIES})

当我编译源代码时,我得到:

demo.cpp:(.text+0x3d3): undefined reference to `boost::system::generic_category()'

我检查了拼写(Boost_LIBRARIES 与 BOOST_LIBRARIES)但没问题。

我使用包 boost-devel 在 Fedora 中安装了 boost。

最佳答案

source code 中查找,Boost_LIBRARIES根据传给find_package的组件列表进行填充。尝试:

find_package(Boost REQUIRED COMPONENTS system)

您还应该使用导入的目标:

set(Boost_USE_STATIC_LIBS        ON)
set(Boost_USE_MULTITHREADED      ON)
set(Boost_USE_STATIC_RUNTIME    OFF)
find_package(Boost REQUIRED COMPONENTS system)

# the call to include_directories is now useless:
# the Boost::system imported target used below
# embeds the include directories

project(APP C CXX)
add_executable(APP src.cpp)
target_link_libraries(APP Boost::system)

关于c++ - CMake:对 boost 库的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39513269/

相关文章:

c++ - 获取Boost Deadline_timer native 文件描述符

c++ - 如何在共享对象构建中包含子目录

C++线程安全——读图

c++ - 有没有生成 Makefile 的好工具?

c++ - 如何在其他计算机上部署用visual studio编写的简单boost程序

c++ - 使用局部变量 boost spirit

c++ - 如何在不运行 'make install' 的情况下使用 OpenCV?

gcc - CMake:如何显示编译器的标准输出

c++ - Qt:子类中无法识别继承的信号

c++ - 线程调用类函数错误