0%

CMake教程(五):安装和测试

CMake Help
系列基于CMake Tutorial
本文基于Step 5: Installing and Testing

Exercise 1 - Install Rules

修改MathFunctions/CMakeLists.txt,添加

1
2
3
4
set(installable_libs MathFunctions tutorial_compiler_flags)
# For MathFunctions, we want to install the libraries and header file to the lib and include directories respectively.
install(TARGETS ${installable_libs} DESTINATION lib)
install(FILES mathfunctions.h DESTINATION include)

修改CMakeLists.txt,添加

1
2
3
4
install(FILES "${PROJECT_BINARY_DIR}/MyProject.h"
"include/hello.h"
DESTINATION include
)

运行命令

1
2
3
cmake -B build
cmake --build build
--config Release

执行安装

1
cmake --install build

默认情况下,安装路径为/usr/local,window系统为C:/Program Files/${PROJECT_NAME}。可以设定安装路径:

1
cmake --install build --prefix "/home/myuser/installdir" 

安装成功后,执行命令

1
2
cd /home/myuser/installdir
tree .

可以得到如下文件目录结构

1
2
3
4
5
6
7
8
9
.
├── bin
│   └── target1
├── include
│   ├── MyProject.h
│   ├── hello.h
│   └── mathfunctions.h
└── lib
└── libMathFunctions.a

可执行文件在bin目录中。

Exercise 2 - Testing Support