为什么不包括此提升头文件

时间:2009-06-30 17:41:41

标签: c++ boost cmake compilation

我正在使用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})
在pmake进程中,

Boost包括dirs填充了“/ opt / local / include /”,此文件夹包含一个包含filesystem.hpp的文件夹boost

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: error: boost/filesystem.hpp: No such file or directory
make[2]: *** [src/CMakeFiles/ImageMarker.dir/FaceRecognizer.cpp.o] Error 1

您是否理解为什么编译器没有选择/ opt / local / include目录?

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

3 个答案:

答案 0 :(得分:15)

首先使用

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;
}

答案 1 :(得分:0)

您是否尝试过不包含“boost /”部分的 filesystem.hpp

答案 2 :(得分:0)

我通过在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})