簡體   English   中英

如何使用 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)
  • 安裝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 制作:*** [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})

我現在通過了文件未找到- 錯誤,但我得到以下信息:

/Applications/CLion EAP.app/Contents/bin/cmake/share/cmake-3.1/Modules/FindBoost.cmake:685(文件)中的CMake錯誤:

無法讀取文件字符串文件“/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)

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

它工作得更好,CMake 是為交叉編譯而設計的,在 CMake 項目中給出絕對路徑並不好。

編輯:

也看看這個: How to link C++ program with Boost using CMake

因為您實際上並未將 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 庫。 所以我的解決方案是從以下站點下載到最新版本的 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")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM