I have the following setup for C++ development:
OS X Yosemite
CLion 140.2310.6
(a cross-plattform C/C++-IDE by JetBrains usingCMake
as build system)- installed
boost
viabrew install boost
into/usr/local/Cellar/boost/
Now, my goal is to setup a simple project and include the boost
library. I defined just one test.cpp file that looks like this:
#include <iostream>
#include <boost>
using namespace std;
int test() {
cout << "Hello, World!" << endl;
return 0;
}
My CMakeLists.txt file looks like this:
cmake_minimum_required(VERSION 2.8.4)
project(MyProject)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
include_directories("/usr/local/Cellar/boost/1.57.0/include/boost")
set(SOURCE_FILES main.cpp ./test.cpp)
add_executable(MyProject ${SOURCE_FILES})
When I build the project, I get the following error:
/Users/nburk/Documents/uni/master/master_thesis/MyProject/test.cpp:2:10: fatal error: 'boost' file not found
make[3]: *** [CMakeFiles/MyProject.dir/test.cpp.o] Error 1 make[2]: *** [CMakeFiles/MyProject.dir/all] Error 2 make[1]: *** [CMakeFiles/MyProject.dir/rule] Error 2 make: *** [MyProject] Error 2
I played around with adjusting paths here and there and also using add_library
and target_link_libraries
, none of which made the project build successfully.
Can someone point into the right direction how to make sure I can include boost
s functionality into my CLion C++ project?
Update: Thanks to @Waxo's answer I used the following code in my CMakeLists.txt file which:
set(Boost_INCLUDE_DIR /usr/local/Cellar/boost/1.57.0)
set(Boost_LIBRARY_DIR /usr/local/Cellar/boost/1.57.0/lib)
find_package(Boost COMPONENTS system filesystem REQUIRED)
include_directories(${Boost_INCLUDE_DIR})
I now got past the file not found-error, but instead I get the following:
CMake Error at /Applications/CLion EAP.app/Contents/bin/cmake/share/cmake-3.1/Modules/FindBoost.cmake:685 (file):
file STRINGS file "/usr/local/Cellar/boost/1.57.0/boost/version.hpp" cannot be read.
Call Stack (most recent call first): CMakeLists.txt:11 (find_package)
Any ideas what I am still missing? The referred line (685) in FindBoost.cmake is:
file(STRINGS "${Boost_INCLUDE_DIR}/boost/version.hpp" _boost_VERSION_HPP_CONTENTS REGEX "#define BOOST_(LIB_)?VERSION ")
Boost
variables manually.find_package
should work out of the box. If it does not, you should passBoost_DIR
to thecmake
command. Do not write system-specific paths in the CMakeLists.txt. The whole point of cmake is to be cross-platform. Platform specific configuration should be rarely required, and if it is the way to do that is to pass the configuration parameters via command line or via cmake-gui. – sbabbi/usr/local/cellar
). The proper way to notify cmake to look for boost in that directory is to invoke cmake withcmake -DBOOST_ROOT=/usr/local/Cellar/boost/1.57.0 ...
– sbabbi