(Beginner level; IDE: CLion)
First, install boost from the Cygwin mirror: open the install exe, search for boost, install the packages.
After boost is installed: it will be located in /usr/include/boost
. This is where everything is. All #include
statements will be a path from the boost folder, as in: #include <boost/archive/text_oarchive.hpp>
.
Once you include the boost files of your choice in your .cpp
files, your code will still not compile in your IDE of choice until you link the libraries and tell cmake to search for your downloaded boost code.
In order to get cmake to search for your boost code,
find_package(Boost 1.60.0 COMPONENTS components_you_want)
# for example:
find_package(Boost 1.60.0 COMPONENTS serialization)
Then, include the directories: include_directories(${Boost_INCLUDE_DIRS})
Finally, add your executable and link the libraries:
add_executable(your_target ${SOURCE_FILES})
target_link_libraries(your_target ${Boost_LIBRARIES} -any_missing_boostlibs)
Before starting your program, avoid an error dump by testing to see if boost has been found before including anything or running your code:
if (Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(YourTarget ${SOURCE_FILES})
target_link_libraries(your_target ${Boost_LIBRARIES} -missing_libs)
endif()
I included -missing_libs
because an error you may run into is that some boost library or another might not have been linked, and you must manually add it--for instance, the link I referenced earlier.
All together, a final CMakeLists.txt file might look something like:
cmake_minimum_required(VERSION 3.7)
project(your_project)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp tmap.cpp tmap.h)
find_package(Boost 1.60.0 COMPONENTS serialization)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(your_project ${SOURCE_FILES})
target_link_libraries(your_project ${Boost_LIBRARIES})
endif()