首页 文章

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

提问于
浏览
41

我有以下C开发设置:

  • OS X Yosemite

  • CLion 140.2310.6 (由JetBrains使用 CMake 作为构建系统的跨平台C / C -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; 
}

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

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

/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 ,这些都没有使项目成功构建 .

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

Update: 感谢@ 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})

我现在已经过了 file not found -error,但我得到以下内容:

/ Applications / CLion中的CMake错误EAP.app/Contents/bin/cmake/share/cmake-3.1/Modules/FindBoost.cmake:685(file):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 ")

2 回答

  • 15

    尝试使用CMake find_package(Boost)

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

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

    Edit:

    看看这个也是:How to link C++ program with Boost using CMake

    因为您实际上没有将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()
    
  • 56

    在整个下午度过这个问题之后,我自己解决了这个问题 . 这是一个相当愚蠢的错误,@ 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()
    

相关问题