Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this boost header file not included

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

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:

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

Boost gives the following messages while generating the Makefile, I only copied the boost part:

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

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?

If you need more information I'm happy to provide it

like image 329
Janusz Avatar asked Jun 30 '09 17:06

Janusz


People also ask

Where are Boost header files?

The headers should be in /usr/local/include/boost and the libs should be in /usr/local/lib.

Is Boost library header-only?

Most Boost libraries are header-only: they consist entirely of header files containing templates and inline functions, and require no separately-compiled library binaries or special treatment when linking. The only Boost libraries that must be built separately are: Boost.

How do I add a header file to a compiler?

You can either add a -I option to the command line to tell the compiler to look there for header files. If you have header files in include/ directory, then this command should work for you. There shouldn't be any space between -I compiler option and directory location.

Do I need to include header file in source file?

Generally, you only want to put the minimum necessary includes into a class header file, as anyone else who uses that header will be forced to #include all of them too. In larger projects, this leads towards slower builds, dependency issues, and all sorts of other nastiness.


2 Answers

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;
}
like image 197
Maik Beckmann Avatar answered Sep 20 '22 22:09

Maik Beckmann


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})
like image 43
akakcolin Avatar answered Sep 20 '22 22:09

akakcolin