一、介绍
如先前在第三方库中所述,较新版本的 CMake 允许您使用导入的 ALIAS imported 目标链接第三方库。
本教程中的文件树如下:
$ tree
.
├── CMakeLists.txt
├── main.cpp
- CMakeLists.txt:CMake 指令
- main.cpp:源文件
二、环境版本
- CMake v3.5+
- 安装在默认位置的 boost 库
解析
导入目标
Imported targets 是 FindXXX
模块导出的只读目标。
在 CMake 命令中包含 boost 这个库:
target_link_libraries( imported_targets
PRIVATE
Boost::filesystem
)
作用是自动链接 Boost::filesystem
和 Boost :: system
库,同时还包括 Boost 头文件目录。
三、编译例子
$ mkdir build
$ cd build/
$ cmake ..
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- 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
-- Detecting C compile features
-- Detecting C compile features - 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
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Boost version: 1.58.0
-- Found the following Boost libraries:
-- filesystem
-- system
boost found
-- Configuring done
-- Generating done
-- Build files have been written to: /data/code/01-basic/K-imported-targets/build
$ make
Scanning dependencies of target imported_targets
[ 50%] Building CXX object CMakeFiles/imported_targets.dir/main.cpp.o
[100%] Linking CXX executable imported_targets
[100%] Built target imported_targets
$ ./imported_targets
Hello Third Party Include!
Path is not relative