2009-06-30 27 views
11

我正在用cmake在Mac上構建我的C++程序。編譯器給了我以下錯誤:爲什麼不包括這個增強頭文件

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

觸發錯誤線如下:

#include "boost/filesystem.hpp" 

#include <boost/filesystem.hpp> 

這上面我用的不改錯誤

但是在我的CMakeLists.txt中,我使用以下方式包含提升標題:

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

升壓include目錄中填充有「/選擇/本地/包含/」在cmake的過程和該文件夾中包含其中包含filesystem.hpp

升壓給出以下消息而生成的文件夾升壓Makefile中,我只複製了升壓部分:

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

在運行使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目錄嗎?

如果您需要更多的信息,我很高興能提供其

+0

這看起來不錯,「ls /opt/local/include/boost/filesystem.hpp」是什麼意思? – 2009-06-30 18:41:13

+1

確保include_directories($ {Boost_INCLUDE_DIRS})在_before_任何add_executable或add_library語句到達時列出。 – 2009-06-30 18:47:53

+0

謝謝......這似乎有助於我在編譯時遇到boost錯誤。我在include之前添加了帶有可執行語句的子目錄,不知道cmake會在那個時候應用該子目錄中的所有內容。這解決了至少這個問題 – Janusz 2009-06-30 19:10:51

回答

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安裝中增加find模塊。

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

您是否嘗試過包括filesystem.hpp沒有 「升壓/」 的一部分?

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