简体   繁体   English

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

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

I have the following setup for C++ development:我有以下 C++ 开发设置:

  • OS X Yosemite
  • CLion 140.2310.6 (a cross-plattform C/C++-IDE by JetBrains using CMake as build system) CLion 140.2310.6 (JetBrains 使用CMake作为构建系统的跨平台 C/C++-IDE)
  • installed boost via brew install boost into /usr/local/Cellar/boost/安装boost通过brew install boost/usr/local/Cellar/boost/

Now, my goal is to setup a simple project and include the boost library.现在,我的目标是设置一个简单的项目并包含boost库。 I defined just one test.cpp file that looks like this:我只定义了一个test.cpp文件,如下所示:

#include <iostream>
#include <boost>

using namespace std;

int test() {
    cout << "Hello, World!" << endl;
    return 0; 
}

My CMakeLists.txt file looks like this:我的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}) 

When I build the project, I get the following error:当我构建项目时,出现以下错误:

/Users/nburk/Documents/uni/master/master_thesis/MyProject/test.cpp:2:10: fatal error: 'boost' file not found /Users/nburk/Documents/uni/master/master_thesis/MyProject/test.cpp:2:10: 致命错误: 'boost' 文件未找到

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

I played around with adjusting paths here and there and also using add_library and target_link_libraries , none of which made the project build successfully.我打得四处这里调整的路径和那里,也使用add_librarytarget_link_libraries ,其中没有取得成功的项目构建。

Can someone point into the right direction how to make sure I can include boost s functionality into my CLion C++ project?有人可以指出正确的方向如何确保我可以将boost的功能包含到我的 CLion C++ 项目中吗?

Update: Thanks to @Waxo's answer I used the following code in my CMakeLists.txt file which:更新:感谢@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})

I now got past the file not found -error, but instead I get the following:我现在通过了文件未找到- 错误,但我得到以下信息:

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

file STRINGS file "/usr/local/Cellar/boost/1.57.0/boost/version.hpp" cannot be read.无法读取文件字符串文件“/usr/local/Cellar/boost/1.57.0/boost/version.hpp”。

Call Stack (most recent call first): CMakeLists.txt:11 (find_package)调用堆栈(最近调用优先):CMakeLists.txt:11 (find_package)

Any ideas what I am still missing?任何想法我仍然缺少什么? The referred line (685) in FindBoost.cmake is: file(STRINGS "${Boost_INCLUDE_DIR}/boost/version.hpp" _boost_VERSION_HPP_CONTENTS REGEX "#define BOOST_(LIB_)?VERSION ") FindBoost.cmake 中引用的行 (685) 是: file(STRINGS "${Boost_INCLUDE_DIR}/boost/version.hpp" _boost_VERSION_HPP_CONTENTS REGEX "#define BOOST_(LIB_)?VERSION ")

After spending the whole afternoon on the issue, I solved it myself.在这个问题上花了整个下午之后,我自己解决了。 It was a rather stupid mistake and all the hints in @Waxo's answer were really helpful.这是一个相当愚蠢的错误,@Waxo 答案中的所有提示都非常有帮助。

The reason why it wasn't working for me that I wrote #include <boost> within my test.cpp -file, which apparently is just wrong.我在test.cpp文件中写了#include <boost>不起作用的原因,这显然是错误的。 Instead, you need to refer directly to the header files that you actually want to include, so you should rather write eg #include <boost/thread.hpp> .相反,您需要直接引用您实际想要包含的头文件,因此您应该编写例如#include <boost/thread.hpp>

After all, a short sequence of statements should be enough to successfully (and platform-independently) include boost into a CMake project:毕竟,简短的语句序列应该足以成功(并且独立于平台)将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})

These lines are doing the magic here.这些线条在这里发挥着神奇的作用。 For reference, here is a complete CMakeLists.txt file that I used for debugging in a separate command line project:作为参考,这里有一个完整的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()

Try using CMake find_package(Boost)尝试使用 CMake find_package(Boost)

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

It works better and CMake is made for cross compilation and giving an absolute path is not good in a CMake project.它工作得更好,CMake 是为交叉编译而设计的,在 CMake 项目中给出绝对路径并不好。

Edit:编辑:

Look at this one too : How to link C++ program with Boost using CMake也看看这个: How to link C++ program with Boost using CMake

Because you don't link actually the boost library to your executable.因为您实际上并未将 boost 库链接到您的可执行文件。

CMake with boost usually looks like that :带有 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()

I couldn't find boost library using find_package() of clion.我无法使用 clion 的 find_package() 找到 boost 库。 So my solution is download to the latest version boost from the following site:所以我的解决方案是从以下站点下载到最新版本的 boost:

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

After that, extract it and navigate to that folder in CMakeList.txt to include boost library.之后,将其解压缩并导航到CMakeList.txt该文件夹以包含 boost 库。

include_directories("/path_to_external_library")

In my case, I use linux and so I put it under /usr/share/.就我而言,我使用 linux,所以我把它放在 /usr/share/ 下。

include_directories("/usr/share/boost_1_66_0")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM