如何将外部库(boost)添加到与CMake的CLion c++项目中?

时间:2022-07-31 12:44:10

I have the following setup for C++ development:

对于c++开发,我有如下设置:

  • OS X Yosemite
  • OS X约塞米蒂
  • CLion 140.2310.6 (a cross-plattform C/C++-IDE by JetBrains using CMake as build system)
  • CLion 140.2310.6(使用CMake作为构建系统的JetBrains的交叉plattform C/ c++ -IDE)
  • installed boost via brew install boost into /usr/local/Cellar/boost/
  • 安装boost通过brew安装boost进入/usr/local/Cellar/boost/。

Now, my goal is to setup a simple project and include the boost library. I defined just one test.cpp file that looks like this:

现在,我的目标是建立一个简单的项目,包括boost库。我只定义了一个测试。像这样的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

/用户/ nburk /文件/大学/主/ master_thesis / MyProject /测试。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

[3]:* * *(CMakeFiles / MyProject.dir / test.cpp。错误1 make[2]: *** [cmakefile /MyProject。错误2 make[1]: *** [cmakefile /MyProject。错误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_library和target_link_libraries,这些都没有成功地构建项目。

Can someone point into the right direction how to make sure I can include boosts functionality into my CLion C++ project?

有人能指出正确的方向吗?如何确保在我的CLion c++项目中加入boost功能?

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

CMake错误在/应用/CLion EAP.app/内容/bin/ CMake /share/ CMake -3.1/模块/FindBoost。cmake:685(文件):

file STRINGS file "/usr/local/Cellar/boost/1.57.0/boost/version.hpp" cannot be read.

文件的字符串文件“/ usr /地方/地窖/提高/ 1.57.0 /提高/版本。高压泵”不能读。

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中引用的行(685)。cmake是:文件(字符串" $ { Boost_INCLUDE_DIR } /提高/版本。hpp" _boost_VERSION_HPP_CONTENTS REGEX "#define BOOST_(LIB_)?版本”)

2 个解决方案

#1


50  

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. Instead, you need to refer directly to the header files that you actually want to include, so you should rather write e.g. #include <boost/thread.hpp>.

我在测试中写了#include 。cppfile,显然是错误的。相反,您需要直接引用您实际想要包含的头文件,所以您应该更愿意编写例如#include

After all, a short sequence of statements should be enough to successfully (and platform-independently) include boost into a CMake project:

毕竟,一个简短的语句序列应该足够成功(并且独立于平台)包括推进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()

#2


15  

Try using CMake find_package(Boost)

尝试使用CMake find_package(提高)

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

src: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

看看这个:如何使用CMake来链接c++程序。

Because you don't link actually the boost library to your executable.

因为你没有把boost库链接到可执行文件。

CMake with boost usually looks like that :

CMake与boost通常是这样的:

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

#1


50  

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. Instead, you need to refer directly to the header files that you actually want to include, so you should rather write e.g. #include <boost/thread.hpp>.

我在测试中写了#include 。cppfile,显然是错误的。相反,您需要直接引用您实际想要包含的头文件,所以您应该更愿意编写例如#include

After all, a short sequence of statements should be enough to successfully (and platform-independently) include boost into a CMake project:

毕竟,一个简短的语句序列应该足够成功(并且独立于平台)包括推进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()

#2


15  

Try using CMake find_package(Boost)

尝试使用CMake find_package(提高)

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

src: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

看看这个:如何使用CMake来链接c++程序。

Because you don't link actually the boost library to your executable.

因为你没有把boost库链接到可执行文件。

CMake with boost usually looks like that :

CMake与boost通常是这样的:

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