CMake基础 第8节 包含第三方库
介紹#
幾乎所有重要的項目都需要包含第三方庫、頭文件或程序。CMake支持使用find_package()函數查找這些工具的路徑。這將從CMAKE_MODULE_PATH中的文件夾列表中搜索格式為FindXXX.cmake的CMake模塊。在Linux上,默認搜索路徑將包含/usr/share/cmake/Modules。在我的系統上,這包括對大約1420個通用第三方庫的支持。
本教程中的文件如下:
$ tree . ├── CMakeLists.txt ├── main.cpp-
[CMakeLists.txt] - 包含要運行的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庫安裝在默認系統位置。
sudo apt-get install libboost-all-dev -y概念#
查找一個包#
如上所述,find_package()函數將從CMAKE_MODULE_PATH中的文件夾列表中搜索格式為FindXXX.cmake的CMake模塊。find_package的參數的確切格式將取決于你要查找的模塊。這通常記錄在文件FindXXX.cmake的頂部
下面是查找Boost的基本示例:
find_package(Boost 1.46.1 REQUIRED COMPONENTS filesystem system)這些參數是:
- Boost -庫的名稱。這是用于查找模塊文件FindBoost.cmake的一部分。
- 1.46.1 - 要查找的Boost的最低版本。
- REQUIRED - 告訴模塊這是必需的,如果失敗,則找不到該模塊。
- COMPONENTS - 要查找的庫列表。
Boost includes可以接受更多參數,還可以利用其他變量。更復雜的設置將在后面的示例中提供。
檢查是否找到該包#
大多數包含的軟件包都會設置一個變量XXX_FOUND,該變量可用于檢查該軟件包在系統上是否可用。
在本例中,變量為BOOST_FOUND:
if(Boost_FOUND)message ("boost found")include_directories(${Boost_INCLUDE_DIRS}) else()message (FATAL_ERROR "Cannot find Boost") endif()導出變量#
在找到包之后,它通常會導出變量,這些變量可以告訴用戶在哪里可以找到庫、頭文件或可執行文件。與XXX_FOUND變量類似,它們是特定于包的,通常記錄在FindXXX.cmake文件的頂部。
本例中導出的變量包括:
- Boost_INCLUDE_DIRS?- Boost頭文件的路徑
在某些情況下,你還可以通過使用ccmake或cmake-gui檢查緩存來檢查這些變量。
別名/導入目標#
大多數現代CMake庫在其模塊文件中導出別名目標。導入目標的好處在于,它們還可以填充頭文件目錄和鏈接庫。
例如,從CMake的3.5版開始,Boost模塊就支持此功能。
類似于將你自己的別名目標用于庫,模塊中的別名可以讓引用找到的目標變得更容易。
在Boost的例子中,所有目標通過使用標識符Boost::加子模塊的名字來導出。例如,你可以使用:
- Boost::boost?僅適用于庫的頭文件
- Boost::system?對于Boost系統庫
- Boost::filesystem?對于文件系統庫
與你自己的目標一樣,這些目標包含它們的依賴項,因此鏈接到?Boost::filesystem?將自動添加?Boost::boost和Boost::system依賴。
要鏈接到導入的目標,可以使用以下命令:
target_link_libraries( third_party_includePRIVATEBoost::filesystem)非別名目標#
雖然大多數現代庫使用導入的目標,但并非所有模塊都已更新。在庫尚未更新的情況下,你通常會發現以下變量可用:
- 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} )構建示例#
$ 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總結
以上是生活随笔為你收集整理的CMake基础 第8节 包含第三方库的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CMake基础 第7节 编译标志
- 下一篇: windows下 VScode+CMak