Why is this boost header file not included

后端 未结 3 1588
旧巷少年郎
旧巷少年郎 2021-01-01 20:01

I\'m building my c++ program with cmake on a Mac. The compiler gives me following Error:

error: boost/filesystem.hpp: No such file or directory
相关标签:
3条回答
  • 2021-01-01 20:28

    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

    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.

    PS: An example

    The 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 讨论(0)
  • 2021-01-01 20:31

    Have you tried including filesystem.hpp without the "boost/" part?

    0 讨论(0)
  • 2021-01-01 20:33

    I solved a similar problem by adding some lines in my 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})
    
    0 讨论(0)
提交回复
热议问题