第三步,安装与测试
下面,我们将为我们的项目添加安装规则,以及测试支持。
安装
相关代码:3_install
安装规则非常直观,只需对想要安装的文件,在项目以及子模块中的 CMakeLists.txt
中指定相应路径即可。
MathFunctions/CMakeLists.txt
对 MathFunctions
库,我们通过向 MathFunctions
的 CMakeLists.txt
文件(注,不是顶级 CMakeLists.txt
)加入如下两行,实现对库和头文件的安装规则设置:
install (TARGETS MathFunctions DESTINATION bin)
install (FILES MathFunctions.h DESTINATION include)
CMakeLists.txt
对整个应用程序,加入如下行到顶级 CMakeLists.txt
文件,来实现对可执行文件以及配置头文件进行安装。
# add the install targets
install (TARGETS Tutorial DESTINATION bin)
install (FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h"
DESTINATION include)
这就是全部。这时候,你应当可以编译这个项目,然后运行 make install
(或者从IDE编译INSTALL目标),然后相应的头文件、库、可执行文件将会被安装。
CMake
变量 CMAKE_INSTALL_PREFIX
用来确定文件被安装的根路径, /usr/local/
是默认的安装路径。
实践
假设我们设置安装路径为 myprefix
, 那么进行过程大致如下:
$mkdir myprefix
$ls -p
myprefix/ tree.cmakelog tree.installlog tree.origin tutorial/
$tree . >tree.origin
$cat tree.origin
.
├── myprefix
├── tree.cmakelog
├── tree.installlog
├── tree.origin
└── tutorial
├── CMakeLists.txt
├── MathFunctions
│ ├── CMakeLists.txt
│ ├── MathFunctions.h
│ └── mysqrt.cpp
├── TutorialConfig.h.in
└── tutorial.cpp
3 directories, 9 files
$cmake -DCMAKE_INSTALL_PREFIX=$(pwd)/myprefix tutorial/
$tree . >tree.cmakelog
$make
Scanning dependencies of target MathFunctions
[ 50%] Building CXX object MathFunctions/CMakeFiles/MathFunctions.dir/mysqrt.cpp.o
Linking CXX static library libMathFunctions.a
[ 50%] Built target MathFunctions
Scanning dependencies of target Tutorial
[100%] Building CXX object CMakeFiles/Tutorial.dir/tutorial.cpp.o
Linking CXX executable Tutorial
[100%] Built target Tutorial
$make install
[ 50%] Built target MathFunctions
[100%] Built target Tutorial
Install the project...
-- Install configuration: ""
-- Installing: /home/miracle/mydata/orgmode/data/f80366ab-6a10-4de9-9793-3a531afbc192/cmake/3_install/myprefix/bin/Tutorial
-- Installing: /home/miracle/mydata/orgmode/data/f80366ab-6a10-4de9-9793-3a531afbc192/cmake/3_install/myprefix/include/TutorialConfig.h
-- Installing: /home/miracle/mydata/orgmode/data/f80366ab-6a10-4de9-9793-3a531afbc192/cmake/3_install/myprefix/bin/libMathFunctions.a
-- Installing: /home/miracle/mydata/orgmode/data/f80366ab-6a10-4de9-9793-3a531afbc192/cmake/3_install/myprefix/include/MathFunctions.h
$tree myprefix >tree.install
$cat tree.install
myprefix/
├── bin
│ ├── libMathFunctions.a
│ └── Tutorial
└── include
├── MathFunctions.h
└── TutorialConfig.h
2 directories, 4 files
安装文件在 install_manifest.txt
也有指出。另外除了命令行中用 -DCMAKE_INSTALL_PREFIX=<install_path>
指定前缀外,还可在 CMakeLists.txt
中使用 SET(CMAKE_INSTALL_PREFIX <install_path>)
来指定。
常用过程
代码:
$ tree
.
└── tutorial
├── CMakeLists.txt
├── MathFunctions
│ ├── CMakeLists.txt
│ ├── MathFunctions.h
│ └── mysqrt.cpp
├── TutorialConfig.h.in
└── tutorial.cpp
2 directories, 6 files
常用命令:
$mkdir build && cd build
$cmake -DCMAKE_INSTALL_PREFIX=$(pwd)/myprefix tutorial/
$make
$make install
$ls
CMakeCache.txt CMakeFiles cmake_install.cmake install_manifest.txt Makefile MathFunctions myprefix Tutorial TutorialConfig.h
$tree myprefix/
myprefix/
├── bin
│ ├── libMathFunctions.a
│ └── Tutorial
└── include
├── MathFunctions.h
└── TutorialConfig.h
2 directories, 4 files
网友评论