如何使用CMake将外部库(boost)包含到CLion C++项目中
How to include external library (boost) into CLion C++ project with CMake?
我有以下C++开发设置:
OS X Yosemite
CLion 140.2310.6
(JetBrains使用CMake
作为构建系统的跨平台C/C++-IDE)- 通过
brew install boost
将boost
安装到/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_library
和target_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")
- 无法在 CLion 中构建 C++ 项目
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- 从包含m行的文件中提取n行,必要时(惰性地)重复该文件
- 编译包含字符串的代码时遇到问题
- c++库的公共头文件中应该包含什么
- 将包含C样式数组的对象初始化为成员变量(C++)
- 是否需要删除包含对象的"pair"?
- 函数何时会在c++中包含stack_Unwind_Resume调用
- 如何将包含epoch时间的十六进制字符串转换为time_t
- 使用mongocxx驱动程序时包含头文件问题
- 如何在CLion中包含ASIO C++?
- CLion 不索引任何外部包含路径
- 通过 CMake 将外部库包含在 CLion 项目中
- CLion 无法解析类型 std::unordered_map,即使它提示我包含标头并且编译工作
- C++ 当您需要包含OpenGL库时,如何在Clion中运行程序
- 为什么 CLion (cmake) 不需要包含<string>来构建,而 VS 需要?
- 如何为所有 CLion 包含设置单个根
- 如何使用CMake将外部库(boost)包含到CLion C++项目中
- CLion IDE包含ReSharper C++在Visual Studio下提供的所有功能吗
- 包含库的c++运行文件失败而没有编译错误(CMake / CLion)