C++ 为什么不包括这个boost头文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1065011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is this boost header file not included
提问by Janusz
I'm building my c++ program with cmake on a Mac. The compiler gives me following Error:
我正在 Mac 上使用 cmake 构建我的 c++ 程序。编译器给了我以下错误:
error: boost/filesystem.hpp: No such file or directory
The line that triggers the error is the following:
触发错误的行如下:
#include "boost/filesystem.hpp"
or
或者
#include <boost/filesystem.hpp>
Which of the above I use doesn't changed the Error
我使用的以上哪一个没有改变错误
But in my CMakeLists.txt I include the boost headers in the following way:
但是在我的 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})
Boost include dirs is filled with "/opt/local/include/" during the cmake process and this folder contains a folder boost which contains the filesystem.hpp
在 cmake 过程中,Boost 包含目录中填充了“/opt/local/include/”,并且该文件夹包含一个文件夹 boost,其中包含 filesystem.hpp
Boost gives the following messages while generating the Makefile, I only copied the boost part:
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
While running make VERBOSE=1 This line contains the error:
运行时 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
Do you understand why the compiler isn't picking the /opt/local/include directory?
你明白为什么编译器不选择 /opt/local/include 目录吗?
If you need more information I'm happy to provide it
如果您需要更多信息,我很乐意提供
回答by Maik Beckmann
First of all use
首先使用
FIND_PACKAGE(Boost REQUIRED)
rather than
而不是
FIND_PACKAGE(Boost)
This way cmake will give you a nice error message if it doesn't find it, long before any compilations are started. If it fails set the environment variable BOOST_ROOT to /opt/local (which is the install prefix). Additionally you will have to link in the filesystem library, so you want
这样,如果在任何编译开始之前很久没有找到,cmake 就会给你一个很好的错误消息。如果失败,请将环境变量 BOOST_ROOT 设置为 /opt/local(这是安装前缀)。此外,您必须在文件系统库中进行链接,因此您需要
FIND_PACKAGE(Boost COMPONENTS filesystem REQUIRED)
for later use of
供以后使用
target_link_libraries(mytarget ${Boost_FILESYSTEM_LIBRARY})
Enter
进入
cmake --help-module FindBoost
at the shell to get the docs for the Boost find module in your cmake installation.
在 shell 中获取 cmake 安装中 Boost find 模块的文档。
PS: An example
PS:一个例子
The CMakeLists.txt
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;
}
回答by akakcolin
I solved a similar problem by adding some lines in my CMakeLists.txt.
我通过在 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})
回答by scvalex
Have you tried including filesystem.hppwithout the "boost/" part?
您是否尝试过在没有“boost/”部分的情况下包含filesystem.hpp?