如何使用CMake将外部库(boost)包含到CLion C++项目中?

nbu*_*urk 47 c++ boost cmake clion

我有以下C++开发设置:

  • OS X Yosemite
  • CLion 140.2310.6(一个跨平台的C/C++ - 由JetBrains CMake用作构建系统的IDE )
  • boost通过安装brew install boost/usr/local/Cellar/boost/

现在,我的目标是设置一个简单的项目并包含boost库.我只定义了一个看起来像这样的test.cpp文件:

#include <iostream>
#include <boost>

using namespace std;

int test() {
    cout << "Hello, World!" << endl;
    return 0; 
}
Run Code Online (Sandbox Code Playgroud)

我的CMakeLists.txt文件如下所示:

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}) 
Run Code Online (Sandbox Code Playgroud)

当我构建项目时,我收到以下错误:

/Users/nburk/Documents/uni/master/master_thesis/MyProject/test.cpp:2:10:致命错误:找不到'boost'文件

make [3]:***[CMakeFiles/MyProject.dir/test.cpp.o]错误1 make [2]:***[CMakeFiles/MyProject.dir/all]错误2 make [1]:***[CMakeFiles/MyProject.dir/rule]错误2 make:***[MyProject]错误2

我在这里和那里调整路径,并使用add_librarytarget_link_libraries,没有一个使项目成功构建.

有人能指出正确的方向如何确保我可以boost在我的CLion C++项目中包含功能吗?

更新: 感谢@ Waxo的回答我在我的CMakeLists.txt文件中使用了以下代码:

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})
Run Code Online (Sandbox Code Playgroud)

我现在已经找到了未找到文件 -error,但我获得了以下内容:

/ Applications/CLion中的CMake错误EAP.app/Contents/bin/cmake/share/cmake-3.1/Modules/FindBoost.cmake:685(file):

文件STRINGS文件"/usr/local/Cellar/boost/1.57.0/boost/version.hpp"无法读取.

调用堆栈(最近一次调用):CMakeLists.txt:11(find_package)

我还缺少什么想法?FindBoost.cmake中的引用行(685)是: file(STRINGS "${Boost_INCLUDE_DIR}/boost/version.hpp" _boost_VERSION_HPP_CONTENTS REGEX "#define BOOST_(LIB_)?VERSION ")

nbu*_*urk 65

在整个下午花了这个问题后,我自己解决了.这是一个相当愚蠢的错误,@ Waxo答案中的所有提示都非常有用.

#include <boost>test.cpp -file中编写的对我不起作用的原因显然是错误的.相反,您需要直接引用您实际想要包含的头文件,因此您应该编写例如#include <boost/thread.hpp>.

毕竟,一小段语句应足以成功(并且平台独立)包含boostCMake项目中:

find_package(Boost 1.57.0 COMPONENTS system filesystem REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(BoostTest main.cpp)
target_link_libraries(BoostTest ${Boost_LIBRARIES})
Run Code Online (Sandbox Code Playgroud)

这些线在这里发挥着神奇的作用.作为参考,这是一个完整的CMakeLists.txt文件,我在单独的命令行项目中用于调试:

cmake_minimum_required(VERSION 2.8.4)

project(BoostTest)

message(STATUS "start running cmake...")

find_package(Boost 1.57.0 COMPONENTS system filesystem REQUIRED)

if(Boost_FOUND)

    message(STATUS "Boost_INCLUDE_DIRS: ${Boost_INCLUDE_DIRS}")
    message(STATUS "Boost_LIBRARIES: ${Boost_LIBRARIES}")
    message(STATUS "Boost_VERSION: ${Boost_VERSION}")

    include_directories(${Boost_INCLUDE_DIRS})

endif()

add_executable(BoostTest main.cpp)

if(Boost_FOUND)

    target_link_libraries(BoostTest ${Boost_LIBRARIES})

endif()
Run Code Online (Sandbox Code Playgroud)

  • 惊人的回答,谢谢!对于OS X用户:当您使用Homebrew安装Boost时,这也将起作用. (7认同)

Wax*_*axo 17

尝试使用CMake find_package(Boost)

src:http://www.cmake.org/cmake/help/v3.0/module/FindBoost.html

它运行得更好,CMake用于交叉编译,并且在CMake项目中给出绝对路径并不好.

编辑:

看看这个:如何使用CMake将C++程序与Boost链接起来

因为您实际上没有将boost库链接到您的可执行文件.

带有提升的CMake通常看起来像这样:

set(Boost_USE_STATIC_LIBS        ON) # only find static libs
set(Boost_USE_MULTITHREADED      ON)
set(Boost_USE_STATIC_RUNTIME    OFF)
find_package(Boost 1.57.0 COMPONENTS date_time filesystem system ...)
if(Boost_FOUND)
  include_directories(${Boost_INCLUDE_DIRS})
  add_executable(foo foo.cc)
  target_link_libraries(foo ${Boost_LIBRARIES})
endif()
Run Code Online (Sandbox Code Playgroud)

  • 理论上你不必设置Boost_INCLUDE_DIR和Boost_LIBRARY_DIR,它是find_package(Boost)行的工作.不要忘记链接线.如果错误仍然存​​在,则可以查看文件的权限. (2认同)