【问题标题】:Why is this boost header file not included为什么不包含这个boost头文件
【发布时间】:2010-11-07 02:12:44
【问题描述】:

我正在 Mac 上使用 cmake 构建我的 c++ 程序。编译器给了我以下错误:

error: boost/filesystem.hpp: No such file or directory

触发错误的行如下:

#include "boost/filesystem.hpp"

#include <boost/filesystem.hpp>

以上我使用的哪个没有改变错误

但在我的 CMakeLists.txt 中,我通过以下方式包含了 boost 标头:

FIND_PACKAGE(Boost) 
MESSAGE("Boost information:") 
MESSAGE("  Boost_INCLUDE_DIRS: ${Boost_INCLUDE_DIRS}") 
MESSAGE("  Boost_LIBRARIES: ${Boost_LIBRARIES}") 
MESSAGE("  Boost_LIBRARY_DIRS: ${Boost_LIBRARY_DIRS}") 

INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
LINK_DIRECTORIES(${Boost_LIBRARY_DIRS})

在 cmake 过程中,Boost 包含目录被“/opt/local/include/”填充,这个文件夹包含一个文件夹 boost,其中包含 filesystem.hpp

Boost 在生成 Makefile 时给出以下消息,我只复制了 boost 部分:

-- Boost version: 1.38.0
-- Found the following Boost libraries:
Boost information:
Boost_INCLUDE_DIRS: /opt/local/include
Boost_LIBRARIES: 
Boost_LIBRARY_DIRS: /opt/local/lib
-- Configuring done

运行时 make VERBOSE=1 这行包含错误:

cd /Users/janusz/Documents/workspace/ImageMarker/Debug/src && /usr/bin/c++ -O3 -Wall -Wno-deprecated -g -verbose -I/Users/janusz/Documents/workspace/ImageMarker/src/。 -o CMakeFiles/ImageMarker.dir/FaceRecognizer.cpp.o -c /Users/janusz/Documents/workspace/ImageMarker/src/FaceRecognizer.cpp /Users/janusz/Documents/workspace/ImageMarker/src/FaceRecognizer.cpp:8:32:错误:boost/filesystem.hpp:没有这样的文件或目录 make[2]: *** [src/CMakeFiles/ImageMarker.dir/FaceRecognizer.cpp.o] 错误 1

你明白为什么编译器没有选择 /opt/local/include 目录吗?

如果您需要更多信息,我很乐意提供

【问题讨论】:

  • 这看起来不错,“ls /opt/local/include/boost/filesystem.hpp”怎么说?
  • 确保在 到达任何 add_executable 或 add_library 语句之前列出了 include_directories(${Boost_INCLUDE_DIRS})。
  • 谢谢...这似乎有助于我在编译时遇到 boost 错误。我在包含之前添加了带有可执行语句的子目录,不知道cmake 会在那个时候应用该子目录中的所有内容。至少解决了这个问题

标签: c++ boost cmake compilation


【解决方案1】:

首先使用

FIND_PACKAGE(Boost REQUIRED)

而不是

  FIND_PACKAGE(Boost)

这样,如果 cmake 没有找到它,它会在任何编译开始之前很久就给你一个很好的错误消息。如果失败,请将环境变量 BOOST_ROOT 设置为 /opt/local(这是安装前缀)。 此外,您必须链接文件系统库,所以您想要

FIND_PACKAGE(Boost COMPONENTS filesystem REQUIRED)

供以后使用

target_link_libraries(mytarget ${Boost_FILESYSTEM_LIBRARY})

输入

cmake --help-module FindBoost

在 shell 中获取 cmake 安装中 Boost 查找模块的文档。

PS:一个例子

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
project(Foo)

find_package(Boost COMPONENTS filesystem REQUIRED)

include_directories(${Boost_INCLUDE_DIRS})
add_executable(foo main.cpp)
target_link_libraries(foo 
  ${Boost_FILESYSTEM_LIBRARY}
)

main.cpp

#include <boost/filesystem.hpp>
#include <vector>
#include <string>
#include <cstdio>
#include <cstddef>

namespace fs = boost::filesystem;
using namespace std;

int main(int argc, char** argv)
{
  vector<string> args(argv+1, argv+argc);
  if(args.empty())
  {
    printf("usage: ./foo SOME_PATH\n");
    return EXIT_FAILURE;
  }

  fs::path path(args.front());

  if(fs::exists(path))
    printf("%s exists\n", path.string().c_str());
  else
    printf("%s doesn't exist\n", path.string().c_str());

  return EXIT_SUCCESS;
}

【讨论】:

  • Boost_INCLUDE_DIRS: /opt/local/include
  • 请运行 $ make VERBOSE=1 并告诉我们编译失败的文件所在的行
  • 请使用pastebin.ca 向我们发送您的操作完整日志的链接:)
  • 添加了 make 的输出。在编译器调用期间似乎没有提到 /opt/local/include
【解决方案2】:

我通过在我的 CMakeLists.txt 中添加一些行来解决类似的问题。

cmake_minimum_required(VERSION 3.5)
project(MyResource)

function(myresources out_var)
    set(RESULT)
    foreach(in_f ${ARGN})
        file(RELATIVE_PATH src_f ${CMAKE_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/${in_f})
        set(out_f "${PROJECT_BINARY_DIR}/${in_f}.c")
        add_custom_command(OUTPUT ${out_f}
                COMMAND myresource ${out_f} ${src_f}
                DEPENDS ${in_f}
                WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
                COMMENT "Building binary file for ${out_f}"
                VERBATIM)
        list(APPEND result ${out_f})
    endforeach()
    set(${out_var} "${result}" PARENT_SCOPE)
endfunction()

find_package(Boost COMPONENTS filesystem REQUIRED)
find_path(BOOST_FILESYSTEM_INCLUDE_DIRS boost/filesystem.hpp)

add_executable(myresource myresource.cpp)
target_link_libraries(myresource ${Boost_FILESYSTEM_LIBRARY} ${Boost_SYSTEM_LIBRARY})
target_include_directories(myresource PUBLIC ${BOOST_FILESYSTEM_INCLUDE_DIRS})

【讨论】:

    【解决方案3】:

    您是否尝试过包含 filesystem.hpp 而没有“boost/”部分?

    【讨论】:

    • boost 中的所有头文件都会像 #include 那样包含它们,所以如果 不起作用,则什么都不起作用!
    猜你喜欢
    • 2017-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多