CMake使用教程
通过编写CMakeLists.txt,可以控制生成的Makefile,从而控制编译过程。CMake自动生成的Makefile不仅可以通过make命令构建项目生成目标文件,还支持安装(make
install)、测试安装的程序是否能正确执行(make test,或者ctest)、生成当前平台的安装包(make
package)、生成源码包(make
package_source)、产生Dashboard显示数据并上传等高级功能,只要在CMakeLists.txt中简单配置,就可以完成很多复杂的功能,包括写测试用例。

CMake使用步骤:
cmake-2.8.1-win32-x86/bin/cmake-gui.exe

执行Configure:
运行之后,生成了如下文件:

生成Makefile:
执行Generate之后生成如下文件:






install,无法运行make test,但可以参考。)
简单的程序编译。


D:/Projects/Lab/testngpp/cmake-2.8.1/Tests/Tutorial/Step1/build3>make
Linking CXX executable Tutorial.exe
[100%] Built target Tutorial
D:/Projects/Lab/testngpp/cmake-2.8.1/Tests/Tutorial/Step1/build3>Tutorial.exe
Tutorial.exe Version 1.0
Usage: Tutorial.exe number
D:/Projects/Lab/testngpp/cmake-2.8.1/Tests/Tutorial/Step1/build3>Tutorial.exe 4
The square root of 4 is 2
2、Step2
把子目录编译为库,并且链接到最终的可执行文件。
add_subdirectory (MathFunctions) # 使得子目录MathFunctions也能被编译
# add the executable
add_executable (Tutorial tutorial.cxx)
target_link_libraries (Tutorial MathFunctions)
产生makefile:
D:/Projects/Lab/testngpp/cmake-2.8.1/Tests/Tutorial/Step2/build>make
[ 50%] Building CXX object MathFunctions/CMakeFiles/MathFunctions.dir/mysqrt.cxx
.obj
Linking CXX static library libMathFunctions.a
[ 50%] Built target MathFunctions
Linking CXX executable Tutorial.exe
[100%] Built target Tutorial
3、Step3
macro (do_test arg result)
add_test (TutorialComp${arg} Tutorial ${arg})
set_tests_properties (TutorialComp${arg}
PROPERTIES PASS_REGULAR_EXPRESSION ${result})
endmacro (do_test)
do_test (25 "25 is 5")
do_test (-25 "-25 is 0")
执行make install:
D:/Projects/Lab/testngpp/cmake-2.8.1/Tests/Tutorial/Step3/build>make install
[ 50%] "Built target MathFunctions"
[100%] "Built target Tutorial"
Install the project...
-- Install configuration: ""
-- Installing: C:/Program Files/Tutorial/bin/Tutorial.exe
-- Installing: C:/Program Files/Tutorial/include/TutorialConfig.h
-- Installing: C:/Program Files/Tutorial/bin/libMathFunctions.a
-- Installing: C:/Program Files/Tutorial/include/MathFunctions.h
安装结果:
C:/Program Files/Tutorial>tree /f
C:.
├─bin
│ libMathFunctions.a
│ Tutorial.exe
│
└─include
MathFunctions.h
TutorialConfig.h
执行make test:
D:/Projects/Lab/testngpp/cmake-2.8.1/Tests/Tutorial/Step3/build>make test
Running tests...
Test project D:/Projects/Lab/testngpp/cmake-2.8.1/Tests/Tutorial/Step3/build
Start 1: TutorialRuns
1/5 Test #1: TutorialRuns ..................... Passed 0.01 sec
Start 2: TutorialComp25
2/5 Test #2: TutorialComp25 ................... Passed 0.01 sec
Start 3: TutorialNegative
3/5 Test #3: TutorialNegative ................. Passed 0.01 sec
Start 4: TutorialSmall
4/5 Test #4: TutorialSmall .................... Passed 0.00 sec
Start 5: TutorialUsage
5/5 Test #5: TutorialUsage .................... Passed 0.00 sec
修改一个测试用例,让它不过:
修改顶层CMakeLists.txt,然后重新Configure和Generate,然后make test即可看到结果。
D:/Projects/Lab/testngpp/cmake-2.8.1/Tests/Tutorial/Step3/build>make test
Running tests...
Test project D:/Projects/Lab/testngpp/cmake-2.8.1/Tests/Tutorial/Step3/build
Start 1: TutorialRuns
1/5 Test #1: TutorialRuns ..................... Passed 0.01 sec
Start 2: TutorialComp25
2/5 Test #2: TutorialComp25 ...................***Failed Required regular expre
ssion not found.Regex=[25 is 3
] 0.01 sec
Start 3: TutorialNegative
3/5 Test #3: TutorialNegative ................. Passed 0.01 sec
Start 4: TutorialSmall
4/5 Test #4: TutorialSmall .................... Passed 0.01 sec
Start 5: TutorialUsage
5/5 Test #5: TutorialUsage .................... Passed 0.01 sec
2 - TutorialComp25 (Failed)
Errors while running CTest
make: *** [test] Error 8
4、Step4
检查方法:
# does this system provide the log and exp functions?
include (CheckFunctionExists.cmake)
check_function_exists (log HAVE_LOG)
check_function_exists (exp HAVE_EXP)
(2)修改.in文件,定义宏。(修改TutorialConfig.h.in,cmake执行中会把宏定义为合适的值,生成TurorialConfig.h,供编译时使用)
// does the platform provide exp and log functions?
#cmakedefine HAVE_LOG
#cmakedefine HAVE_EXP
#if defined (HAVE_LOG) && defined (HAVE_EXP)
result = exp(log(x)*0.5);
#else // otherwise use an iterative approach
5、Step5
D:/Projects/Lab/testngpp/cmake-2.8.1/Tests/Tutorial/Step5/build>make
Scanning dependencies of target MakeTable
[ 25%] Building CXX object MathFunctions/CMakeFiles/MakeTable.dir/MakeTable.cxx.
obj
Linking CXX executable MakeTable.exe
[ 25%] "Built target MakeTable"
[ 50%] Generating Table.h
'.' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
make[2]: *** [MathFunctions/Table.h] Error 1
make[1]: *** [MathFunctions/CMakeFiles/MathFunctions.dir/all] Error 2
make: *** [all] Error 2
问题分析:
MakeTable: cmake_check_build_system
$(MAKE) -f CMakeFiles/Makefile2 MakeTable
.PHONY : MakeTable
再看Makefile2文件,找到出错时正在编译的目标。
MathFunctions/CMakeFiles/MakeTable.dir/all:
$(MAKE) -f MathFunctions/CMakeFiles/MakeTable.dir/build.make MathFunctions/CMakeFiles/MakeTable.dir/depend
$(MAKE) -f MathFunctions/CMakeFiles/MakeTable.dir/build.make MathFunctions/CMakeFiles/MakeTable.dir/build
$(CMAKE_COMMAND) -E cmake_progress_report D:/Projects/Lab/testngpp/cmake-2.8.1/Tests/Tutorial/Step5/build/CMakeFiles 1
@echo "Built target MakeTable"
.PHONY : MathFunctions/CMakeFiles/MakeTable.dir/all
Make规则的执行顺序是按照命令的先后顺序:
echo "First line."
echo "Second line."
那么make结果:
D:/Users/Desktop>make
echo "First line."
First line.
echo "Second line."
Second line.
6、Step6
D:/Projects/Lab/testngpp/cmake-2.8.1/Tests/Tutorial/Step6/build>make package
[ 50%] "Built target MathFunctions"
[100%] "Built target Tutorial"
Run CPack packaging tool...
CPack Error: Cannot find NSIS registry value. This is usually caused by NSIS not
being installed. Please install NSIS from http://nsis.sourceforge.net
CPack Error: Cannot initialize the generator NSIS
make: *** [package] Error 1
D:/Projects/Lab/testngpp/cmake-2.8.1/Tests/Tutorial/Step6/build>make package
[ 50%] "Built target MathFunctions"
[100%] "Built target Tutorial"
Run CPack packaging tool...
CPack: Create package using NSIS
CPack: Install projects
CPack: - Run preinstall target for: Tutorial
CPack: - Install project: Tutorial
CPack: Compress package
CPack: Finalize package
CPack: Package D:/Projects/Lab/testngpp/cmake-2.8.1/Tests/Tutorial/Step6/build/T
utorial-1.0.1-win32.exe generated.


D:/Projects/Lab/testngpp/cmake-2.8.1/Tests/Tutorial/Step6/build>make package_sou
rce
Run CPack packaging tool for source...
CPack Error: Cannot find a suitable ZIP program
CPack Error: Cannot initialize the generator ZIP
make: *** [package_source] Error 1

ental
Site: JELLY-PC2
Build name: Win32-make
Create new tag: 20100521-1833 - Experimental
Configure project
Each . represents 1024 bytes of output
. Size of output: 0K
Build project
Each symbol represents 1024 bytes of output.
'!' represents an error and '*' a warning.
. Size of output: 0K
0 Compiler errors
0 Compiler warnings
Test project D:/Projects/Lab/testngpp/cmake-2.8.1/Tests/Tutorial/Step7/build
Start 1: TutorialRuns
1/9 Test #1: TutorialRuns ..................... Passed 0.01 sec
Start 2: TutorialUsage
2/9 Test #2: TutorialUsage .................... Passed 0.01 sec
Start 3: TutorialComp4
3/9 Test #3: TutorialComp4 .................... Passed 0.01 sec
Start 4: TutorialComp9
4/9 Test #4: TutorialComp9 .................... Passed 0.01 sec
Start 5: TutorialComp5
5/9 Test #5: TutorialComp5 .................... Passed 0.01 sec
Start 6: TutorialComp7
6/9 Test #6: TutorialComp7 .................... Passed 0.01 sec
Start 7: TutorialComp25
7/9 Test #7: TutorialComp25 ................... Passed 0.01 sec
Start 8: TutorialComp-25
8/9 Test #8: TutorialComp-25 .................. Passed 0.01 sec
Start 9: TutorialComp0.0001
9/9 Test #9: TutorialComp0.0001 ............... Passed 0.01 sec
100% tests passed, 0 tests failed out of 9
Total Test time (real) = 0.19 sec
Performing coverage
Cannot find any coverage files. Ignoring Coverage request.
Submit files (using http)
Using HTTP submit method
Drop site:http://
Error when uploading file: D:/Projects/Lab/testngpp/cmake-2.8.1/Tests/Tutoria
l/Step7/build/Testing/20100521-1833/Build.xml
Error message was: couldn't connect to host
Problems when submitting via HTTP
Errors while running CTest

CMake使用教程的更多相关文章
- CMake入门教程(转帖)
本文转自:https://www.cnblogs.com/never--more/p/6921837.html CMake入门教程 参考文献:http://www.ibm.com/developerw ...
- cmake全面教程
1. 官网教程 2. 中文教程 3. Modern CMake
- CMake基础教程
如果需要配置和检查我们工程中的所有依赖,那么可以选用CMake工具:但是,这并不是必须的,因为我们可以使用其他的工具或者是IDE(比如Makefiles或者Visual Studio)来配置我们的工程 ...
- cmake简易教程
用cmake替代makefile,构建项目还是蛮简单实用的. 工程目录下src放源代码,build保存所有的编译过程和结果. 首先看看src目录下的源代码结构: 最顶层CMakeLists.txt内容 ...
- CMake 实践教程
本篇博客是根据 <<CMake Practice>> 一文编写, 目的有三: 其一: 提取出其中的精要部分; 其二: 对其中不易理解的地方进行简要说明; 其三: 方便后续查找复 ...
- Linux下CMake简明教程
转载地址:https://blog.csdn.net/whahu1989/article/details/82078563 CMake是开源.跨平台的构建工具,可以让我们通过编写简单的配置文件去生成本 ...
- [转载] CMake Official Tutorial——教程还是官方的好
CMake官方教程传送门:https://cmake.org/cmake-tutorial/ 以下的内容跟官方教程基本一致,少数地方根据自己的测试有所改动: A Basic Starting Poin ...
- CMake使用总结(转的)+自己的实践心得
来自https://www.mawenbao.com/note/cmake.html 总结CMake的常用命令,并介绍有用的CMake资源. CMake意为cross-platform make,可用 ...
- CMake使用总结
总结CMake的常用命令,并介绍有用的CMake资源. CMake意为cross-platform make,可用于管理c/c++工程.CMake解析配置文件CMakeLists.txt生成Makef ...
随机推荐
- svn 安装 、使用(2)
写在前面的话: p.s.有必要读一读,不然可能会浪费你的时间. 该篇是接着上一篇的,上一篇是看了很多人写的文章,汇总的一些可能的情况,最后还是没有成功.此篇是在一个同学的帮助下成功的,也是在自己做好的 ...
- 【NOIP2013】华容道
看别人的代码然后被坑了一下午+一晚上,睡一觉第二天醒悟过来打表过了 果然打表才是正确的调试方法,跟踪什么的去屎(╯‵□′)╯︵┻━┻ 原题: 小 B 最近迷上了华容道,可是他总是要花很长的时间才能完成 ...
- Java——正则表达式(字符串操作)
public class Test1 { /* * 正则表达式:对字符串的常见操作: * 1.匹配: * 其实是用的就是string类中的matches(匹配)方法. * 2.切割 * 其实 ...
- Abdicate
每次,打开背单词软件,背诵单词时,A打头的单词,第一个,永远会是 Abdicate,放弃,抛弃. 真是有意思. 想想在学习js高级程序设计一书时,一横心,决心手动输入书中出现过的每一个代码片段示例. ...
- Unity3D研究院之使用Animation编辑器编辑动画
Unity提供了Animation编辑器,它可以为我们编辑物理动画.举个例子比如场景中有一个来回摇动的秋千,这个秋千在项目中完全只起到衬托作用,它不会与别的游戏对象有任何交互.如果这个秋千也用代码来 ...
- 遇到EDIUS视频条全变灰色编辑不了如何处理
上次使用EDIUS编辑完的视频,这次再打开时,原素材都还能用,时间线上的视音频却都呈灰色不可编辑状态,要怎么办呢?别慌,小编来告诉你解决视频条全变灰色EDIUS不可编辑的办法. 下图就是视频全变灰色E ...
- IIS6下PHP环境的资源未找到(404)问题
故障现象: 无法找到该页, 404错误
- 无法在web服务器上启动调试。Microsoft Visual Studio 远程调试监视器(MSVSMON.EXE)似乎没有在远程计算机上运行,VS2012调试错误
1.重启(无用) 2.关闭防火墙(无用) 3.开启文件与打印机共享(无用) 4.无远程调试权限,改为本地调试.或者是IIS中此项目没有启动.或者没有在IIS中新建此项目.
- 关于getClass().getClassLoader()
关于getClass().getClassLoader() InputStream is = getClass().getClassLoader().getResourceAsStre ...
- 使用ExpandableListView——当有Group选项展开时,如何正确获取长按的Group选项。
当我们使用ExpandableListView时,实现点击一个GroupView则展开ChidView,那么这个时候,Adapter的大小前后是有变化的. 例如:假设有20个GroupView,每个G ...
