日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

CMake基础 第8节 包含第三方库

發(fā)布時間:2024/4/18 编程问答 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 CMake基础 第8节 包含第三方库 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

介紹#

幾乎所有重要的項(xiàng)目都需要包含第三方庫、頭文件或程序。CMake支持使用find_package()函數(shù)查找這些工具的路徑。這將從CMAKE_MODULE_PATH中的文件夾列表中搜索格式為FindXXX.cmake的CMake模塊。在Linux上,默認(rèn)搜索路徑將包含/usr/share/cmake/Modules。在我的系統(tǒng)上,這包括對大約1420個通用第三方庫的支持。

本教程中的文件如下:

$ tree . ├── CMakeLists.txt ├── main.cpp
  • [CMakeLists.txt] - 包含要運(yùn)行的CMake命令

    cmake_minimum_required(VERSION 3.5)# Set the project name project (third_party_include)# find a boost install with the libraries filesystem and system find_package(Boost 1.46.1 REQUIRED COMPONENTS filesystem system)# check if boost was found if(Boost_FOUND)message ("boost found") else()message (FATAL_ERROR "Cannot find Boost") endif()# Add an executable add_executable(third_party_include main.cpp)# link against the boost libraries target_link_libraries( third_party_includePRIVATEBoost::filesystem )
  • [main.cpp] - 具有main的源文件

    #include <iostream> #include <boost/shared_ptr.hpp> #include <boost/filesystem.hpp>int main(int argc, char *argv[]) {std::cout << "Hello Third Party Include!" << std::endl;// use a shared ptrboost::shared_ptr<int> isp(new int(4));// trivial use of boost filesystemboost::filesystem::path path = "/usr/share/cmake/modules";if(path.is_relative()){std::cout << "Path is relative" << std::endl;}else{std::cout << "Path is not relative" << std::endl;}return 0; }

要求#

此示例要求將Boost庫安裝在默認(rèn)系統(tǒng)位置。

sudo apt-get install libboost-all-dev -y

概念#

查找一個包#

如上所述,find_package()函數(shù)將從CMAKE_MODULE_PATH中的文件夾列表中搜索格式為FindXXX.cmake的CMake模塊。find_package的參數(shù)的確切格式將取決于你要查找的模塊。這通常記錄在文件FindXXX.cmake的頂部

下面是查找Boost的基本示例:

find_package(Boost 1.46.1 REQUIRED COMPONENTS filesystem system)

這些參數(shù)是:

  • Boost -庫的名稱。這是用于查找模塊文件FindBoost.cmake的一部分。
  • 1.46.1 - 要查找的Boost的最低版本。
  • REQUIRED - 告訴模塊這是必需的,如果失敗,則找不到該模塊。
  • COMPONENTS - 要查找的庫列表。

Boost includes可以接受更多參數(shù),還可以利用其他變量。更復(fù)雜的設(shè)置將在后面的示例中提供。

檢查是否找到該包#

大多數(shù)包含的軟件包都會設(shè)置一個變量XXX_FOUND,該變量可用于檢查該軟件包在系統(tǒng)上是否可用。

在本例中,變量為BOOST_FOUND:

if(Boost_FOUND)message ("boost found")include_directories(${Boost_INCLUDE_DIRS}) else()message (FATAL_ERROR "Cannot find Boost") endif()

導(dǎo)出變量#

在找到包之后,它通常會導(dǎo)出變量,這些變量可以告訴用戶在哪里可以找到庫、頭文件或可執(zhí)行文件。與XXX_FOUND變量類似,它們是特定于包的,通常記錄在FindXXX.cmake文件的頂部。

本例中導(dǎo)出的變量包括:

  • Boost_INCLUDE_DIRS?- Boost頭文件的路徑

在某些情況下,你還可以通過使用ccmake或cmake-gui檢查緩存來檢查這些變量。

別名/導(dǎo)入目標(biāo)#

大多數(shù)現(xiàn)代CMake庫在其模塊文件中導(dǎo)出別名目標(biāo)。導(dǎo)入目標(biāo)的好處在于,它們還可以填充頭文件目錄和鏈接庫。

例如,從CMake的3.5版開始,Boost模塊就支持此功能。

類似于將你自己的別名目標(biāo)用于庫,模塊中的別名可以讓引用找到的目標(biāo)變得更容易。

在Boost的例子中,所有目標(biāo)通過使用標(biāo)識符Boost::加子模塊的名字來導(dǎo)出。例如,你可以使用:

  • Boost::boost?僅適用于庫的頭文件
  • Boost::system?對于Boost系統(tǒng)庫
  • Boost::filesystem?對于文件系統(tǒng)庫

與你自己的目標(biāo)一樣,這些目標(biāo)包含它們的依賴項(xiàng),因此鏈接到?Boost::filesystem?將自動添加?Boost::boost和Boost::system依賴。

要鏈接到導(dǎo)入的目標(biāo),可以使用以下命令:

target_link_libraries( third_party_includePRIVATEBoost::filesystem)

非別名目標(biāo)#

雖然大多數(shù)現(xiàn)代庫使用導(dǎo)入的目標(biāo),但并非所有模塊都已更新。在庫尚未更新的情況下,你通常會發(fā)現(xiàn)以下變量可用:

  • xxx_INCLUDE_DIRS - 指向庫的include目錄的變量
  • xxx_LIBRARY - 指向庫路徑的變量.

然后,可以將這些文件添加到target_include_directory和target_link_library中:

# Include the boost headers target_include_directories( third_party_includePRIVATE ${Boost_INCLUDE_DIRS} )# link against the boost libraries target_link_libraries( third_party_includePRIVATE${Boost_SYSTEM_LIBRARY}${Boost_FILESYSTEM_LIBRARY} )

構(gòu)建示例#

$ mkdir build$ cd build/$ cmake .. -- The C compiler identification is GNU 4.8.4 -- The CXX compiler identification is GNU 4.8.4 -- Check for working C compiler: /usr/bin/cc -- Check for working C compiler: /usr/bin/cc -- works -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working CXX compiler: /usr/bin/c++ -- Check for working CXX compiler: /usr/bin/c++ -- works -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Boost version: 1.54.0 -- Found the following Boost libraries: -- filesystem -- system boost found -- Configuring done -- Generating done -- Build files have been written to: /home/matrim/workspace/cmake-examples/01-basic/H-third-party-library/build$ make Scanning dependencies of target third_party_include [100%] Building CXX object CMakeFiles/third_party_include.dir/main.cpp.o Linking CXX executable third_party_include [100%] Built target third_party_include matrim@freyr:~/workspace/cmake-examples/01-basic/H-third-party-library/build$ ./ CMakeFiles/ third_party_include matrim@freyr:~/workspace/cmake-examples/01-basic/H-third-party-library/build$ ./third_party_include Hello Third Party Include! Path is not relative $ cmake .. -- The C compiler identification is GNU 4.8.4 -- The CXX compiler identification is GNU 4.8.4 -- Check for working C compiler: /usr/bin/cc -- Check for working C compiler: /usr/bin/cc -- works -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working CXX compiler: /usr/bin/c++ -- Check for working CXX compiler: /usr/bin/c++ -- works -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Boost version: 1.54.0 -- Found the following Boost libraries: -- filesystem -- system boost found -- Configuring done -- Generating done -- Build files have been written to: /home/matrim/workspace/cmake-examples/01-basic/H-third-party-library/build$ make Scanning dependencies of target third_party_include [100%] Building CXX object CMakeFiles/third_party_include.dir/main.cpp.o Linking CXX executable third_party_include [100%] Built target third_party_include$ ./third_party_include Hello Third Party Include! Path is not relative

總結(jié)

以上是生活随笔為你收集整理的CMake基础 第8节 包含第三方库的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。