Szymon Madera
Szymon Madera

Reputation: 97

How to link to boost libraries using Cmake?

My environment is Windows 7, CLion, MinWG. I am trying to use Boost. I have one CMakeLists.txt file, but lots of troubles... :D

cmake_minimum_required(VERSION 2.8.4)
project(untitled)

set(BOOST_INCLUDEDIR "${BOOST_INCLUDEDIR} C:\\MinGW\\include\\boost")
set(Boost_DIR "${Boost_DIR} C:\\MinGW\\include\\boost")
set(CMAKE_LIBRARY_PATH "${CMAKE_LIBRARY_PATH} C:\\MinGW\\lib")
set(CMAKE_INCLUDE_PATH "${CMAKE_INCLUDE_PATH} C:\\MinGW\\include\\boost")

set(BOOST_USE_STATIC_LIBS   ON)
set(BOOST_USE_MULTITHREADED ON)
#set(BOOST_ADDITIONAL_VERSIONS "1.44" "1.44.0")

find_package(BOOST COMPONENTS thread date_time program_options filesystem system REQUIRED)
if(Boost_FOUND)
message(STATUS "Boost znaleziony")
endif()

include_directories(${BOOST_INCLUDEDIR})
include_directories(C:\\MinGW\\include)
LINK_DIRECTORIES(${Boost_LIBRARY_DIRS})

find_package(Threads REQUIRED)


#if (WIN32 AND __COMPILER_GNU)
#        # mingw-gcc fails to link boost::thread
        add_definitions(-DBOOST_THREAD_USE_LIB)
#endif (WIN32 AND __COMPILER_GNU)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_executable(untitled ${SOURCE_FILES})


target_link_libraries(untitled ${Boost_LIBRARIES})

But output is:

undefined reference to `boost::  ...

What i'm doing wrong?

Upvotes: 1

Views: 1000

Answers (1)

ezaquarii
ezaquarii

Reputation: 1916

You probably didn't provide includes path properly.

Here is a working snippet from my project. Tested on Windows and Linux. I'm linking with system, thread and filesystem modules. Pay attention to variables defined by Boost, such as Boost_INCLUDE_DIRS.

find_package( Boost COMPONENTS system thread filesystem REQUIRED )
include_directories(
    ${CMAKE_CURRENT_BINARY_DIR}
    ${CMAKE_CURRENT_SOURCE_DIR}
    ${Boost_INCLUDE_DIRS}
)
target_link_libraries(target_app
    ${Boost_LIBRARIES}
)

Upvotes: 2

Related Questions