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

How to include external library (boost) into CLion C++ project with CMake?

本文关键字:包含 CLion C++ 项目 boost CMake 何使用 外部      更新时间:2023-10-16

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

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

现在,我的目标是设置一个简单的项目,并包含boost库。我只定义了一个test.cpp文件,如下所示:

#include <iostream>
#include <boost>
using namespace std;
int test() {
    cout << "Hello, World!" << endl;
    return 0; 
}

我的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}) 

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

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

make[3]:***[CMakeFiles/MyProject.dir/test.cpp.o]错误1make[2]:***[CMakeFiles/MyProject.dir/all]错误2make[1]:***[CMakeFiles/MyProject.dir/rule]错误2make:***[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})

我现在通过了文件未找到-错误,但我得到了以下内容:

CMake Error at/Applications/CLionEAP.app/Contents/bin/cmake/share/cmake-3.1/Modules/FindBoost.cmake:685(文件):

无法读取文件STRING文件"/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 ")

花了一下午的时间讨论这个问题后,我自己解决了。这是一个相当愚蠢的错误,@Waxo回答中的所有提示都非常有用。

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

毕竟,一个简短的语句序列应该足以成功地(并且独立于平台)将boost包含到CMake项目中:

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})

这些线条在这里发挥着神奇的作用。作为参考,这里有一个完整的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()

尝试使用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()

使用clion的find_package()找不到boost库。因此,我的解决方案是从以下网站下载到最新版本的增强版:

https://sourceforge.net/projects/boost/files/boost/

之后,提取它并导航到CMakeList.txt中的那个文件夹以包含boost库。

include_directories("/path_to_external_library")

在我的例子中,我使用linux,所以我把它放在/usr/share/下。

include_directories("/usr/share/boost_1_66_0")