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/