使用 boost 时缺少包含文件

问题描述 投票:0回答:1

我在使用 boost 时遇到问题,收到错误:“致命错误 C1083:无法打开包含文件:'boost/algorithm/string.hpp':没有这样的文件或目录”。我按照以下步骤来到这里:

  1. git clone --recursive https://github.com/boostorg/boost.git
  2. bootstrap and then .\b2
  3. 添加了 boost 包含并链接了 cmake 列表中的库,如下所示:
    target_include_directories(${PROJECT_NAME} INTERFACE 
    external/boost/libs/algorithm/include)
    target_link_directories(${PROJECT_NAME} INTERFACE external/boost/libs)
    
  4. 然后尝试使用 boost 字符串算法:
    #include <boost/algorithm/string.hpp>
    

你能帮忙吗? 我尝试使用 vcpkg 并在 Cmakelists 中执行了以下操作:

找到并包含Boost

find_package(Boost REQUIRED)
message(STATUS "Boost_INCLUDE_DIRS: ${Boost_INCLUDE_DIRS}")
message(STATUS "Boost_LIBRARIES: ${Boost_LIBRARIES}")

包含 Boost 标头

target_include_directories(${PROJECT_NAME} INTERFACE ${Boost_INCLUDE_DIRS})

Boost 链接

target_link_libraries(${PROJECT_NAME} INTERFACE Boost::headers)

我仍然遇到同样的问题:

fatal error C1083: Cannot open include file: 'boost/algorithm/string.hpp': No such file or directory

我很确定我检查了包含路径并且看起来是正确的。请问有人可以帮忙吗?

c++ cmake boost
1个回答
0
投票

这是我在构建库时经常使用的示例

CMakeLists.txt

add_library(xmlhandler xmlhandler.cpp)

find_package(Boost REQUIRED COMPONENTS headers)
target_link_libraries(xmlhandler Boost::headers)

然后在

header
文件 (
xmlhandler.hpp
) 中我包含

#include <boost/algorithm/string/trim.hpp>

在此示例中,

xmlhandler
是使用
<boost/algorithm/string/trim.hpp>

的自定义库的名称

希望这有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.