c++ - CMake 无法链接 boost 库

标签 c++ windows boost cmake

CMake 可以找到我安装的 boost 版本 (1.67.0),但每当我尝试链接库时,我都会收到大量“ undefined reference ”。

这是我要运行的程序(在 boost 网站上找到的示例):

#include <boost/regex.hpp>
#include <iostream>
#include <string>

int main(int, char *[])
{
    std::string line;
    boost::regex pat("^Subject: (Re: |Aw: )*(.*)");

    while(std::cin) {
        std::getline(std::cin, line);
        boost::smatch matches;
        if(boost::regex_match(line, matches, pat)) {
            std::cout << matches[2] << '\n';
        }
    }

    return 0;
}

这是我的 CMake 脚本:

cmake_minimum_required( VERSION 3.5 )

project( REGEX )

set( BOOST_ROOT D:/Dev/boost/boost_1_67_0/boost_1_67_0 )
set( Boost_LIBRARY_DIR D:/Dev/boost/boost_1_67_0/boost_1_67_0/stage/lib )
set( Boost_USE_STATIC_LIBS ON )
set( Boost_USE_MULTITHREADED ON )

find_package( Boost REQUIRED )

include_directories( ${Boost_INCLUDE_DIRS} )

add_executable( regex ${PROJECT_SOURCE_DIR}/main.cxx )

target_link_libraries( regex ${Boost_LIBRARIES} )

我需要做什么才能成功地将 boost 库与 cmake 链接起来?

编辑: 我更改了脚本,现在它看起来像这样:

cmake_minimum_required( VERSION 3.5 )

project( REGEX )

set( BOOST_ROOT D:/Dev/boost/boost_1_67_0/boost_1_67_0 )
set( BOOST_INCLUDEDIR D:/Dev/boost/boost_1_67_0/boost_1_67_0/boost )
set( BOOST_LIBRARYDIR D:/Dev/boost/boost_1_67_0/boost_1_67_0/stage/lib )
set( Boost_USE_STATIC_LIBS ON )
set( Boost_USE_MULTITHREADED ON )
set( Boost_DEBUG ON )

find_package( Boost COMPONENTS regex REQUIRED )

include_directories( ${Boost_INCLUDE_DIRS} )

add_executable( regex ${PROJECT_SOURCE_DIR}/main.cxx )

target_link_libraries( regex ${Boost_LIBRARIES} )

在为 boost 启用调试后,我看到 cmake 没有任何目录来搜索库,所以我尝试手动设置它。我刚刚安装了 3.11.1 版的 CMake,但它仍然无法识别正则表达式库。

最佳答案

显然,Boost_LIBRARIES 是空的。 您应该在 find_package(Boost REQUIRED [COMPONENTS components]) 中明确指定要链接的 boost 组件,以便它们出现在 Boost_LIBRARIES 中。

在您的情况下,它应该是 find_package(Boost REQUIRED COMPONENTS regex)

请参阅 cmake-boost 文档:https://cmake.org/cmake/help/v3.8/module/FindBoost.html

关于c++ - CMake 无法链接 boost 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50177627/

相关文章:

c++ - CURLOPT_POSTFIELDS 以奇怪的编码发送数据?

c++ - 矩阵乘法哪个更好? GLM 的重载 * 运算符或直接使用着色器

C# 按住鼠标事件

c++ - 当入口和导出指向不同的节点时,如何使用 boost::graph 来(大约)解决旅行商问题?

c++ - 使用链接器选择 C++ 函数的实现

windows - 开发打印驱动程序

c# - 如何在没有 WMI 的情况下知道两个分区是否在一个物理硬盘中?

c++ - Adobe Adam and Eve,C++ : How to create a clickable button, 轨迹栏、文本输入字段和图像?

c++ - 使用单独的 .h 和 .cpp 文件 boost 序列化

c++ - 容器类型转换