如何使用Cmake链接到boost库?

时间:2015-02-16 18:13:11

标签: cmake g++

我的环境是Windows 7,CLion,MinWG。我正在尝试使用Boost。 我有一个CMakeLists.txt文件,但很多麻烦......: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})

但输出是:

undefined reference to `boost::  ...

我做错了什么?

1 个答案:

答案 0 :(得分:2)

您可能没有正确提供包含路径。

这是我项目的工作片段。在WindowsLinux上进行了测试。我正在与systemthreadfilesystem模块进行关联。注意Boost定义的变量,例如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}
)