CMake 入门实战(下)
来源:互联网 发布:电信网络诈骗题库 编辑:程序博客网 时间:2024/06/05 20:42
安装和测试
本节对应的源代码所在目录:Demo5。
CMake 也可以指定安装规则,以及添加测试。这两个功能分别可以通过在产生 Makefile 后使用 make install
和 make test
来执行。在以前的 GNU Makefile 里,你可能需要为此编写install
和 test
两个伪目标和相应的规则,但在 CMake 里,这样的工作同样只需要简单的调用几条命令。
定制安装规则
首先先在 math/CMakeLists.txt 文件里添加下面两行:
123
# 指定 MathFunctions 库的安装路径install (TARGETS MathFunctions DESTINATION bin)install (FILES MathFunctions.h DESTINATION include)
指明 MathFunctions 库的安装路径。之后同样修改根目录的 CMakeLists 文件,在末尾添加下面几行:
1234
# 指定安装路径install (TARGETS Demo DESTINATION bin)install (FILES "${PROJECT_BINARY_DIR}/config.h"DESTINATION include)
通过上面的定制,生成的 Demo 文件和 MathFunctions 函数库 libMathFunctions.o 文件将会被复制到 /usr/local/bin
中,而 MathFunctions.h 和生成的 config.h 文件则会被复制到 /usr/local/include
中。我们可以验证一下3 3顺带一提的是,这里的 /usr/local/
是默认安装到的根目录,可以通过修改 CMAKE_INSTALL_PREFIX
变量的值来指定这些文件应该拷贝到哪个根目录。:
12345678910111213
[ehome@xman Demo5]$ sudo make install[ 50%] Built target MathFunctions[100%] Built target DemoInstall the project...-- Install configuration: ""-- Installing: /usr/local/bin/Demo-- Installing: /usr/local/include/config.h-- Installing: /usr/local/bin/libMathFunctions.a-- Up-to-date: /usr/local/include/MathFunctions.h[ehome@xman Demo5]$ ls /usr/local/binDemo libMathFunctions.a[ehome@xman Demo5]$ ls /usr/local/includeconfig.h MathFunctions.h
为工程添加测试
添加测试同样很简单。CMake 提供了一个称为 CTest 的测试工具。我们要做的只是在项目根目录的 CMakeLists 文件中调用一系列的 add_test
命令。
12345678910111213141516171819202122232425262728
# 启用测试enable_testing()# 测试程序是否成功运行add_test (test_run Demo 5 2)# 测试帮助信息是否可以正常提示add_test (test_usage Demo)set_tests_properties (test_usagePROPERTIES PASS_REGULAR_EXPRESSION "Usage: .* base exponent")# 测试 5 的平方add_test (test_5_2 Demo 5 2)set_tests_properties (test_5_2PROPERTIES PASS_REGULAR_EXPRESSION "is 25")# 测试 10 的 5 次方add_test (test_10_5 Demo 10 5)set_tests_properties (test_10_5PROPERTIES PASS_REGULAR_EXPRESSION "is 100000")# 测试 2 的 10 次方add_test (test_2_10 Demo 2 10)set_tests_properties (test_2_10PROPERTIES PASS_REGULAR_EXPRESSION "is 1024")
上面的代码包含了四个测试。第一个测试 test_run
用来测试程序是否成功运行并返回 0 值。剩下的三个测试分别用来测试 5 的 平方、10 的 5 次方、2 的 10 次方是否都能得到正确的结果。其中 PASS_REGULAR_EXPRESSION
用来测试输出是否包含后面跟着的字符串。
让我们看看测试的结果:
123456789101112131415
[ehome@xman Demo5]$ make testRunning tests...Test project /home/ehome/Documents/programming/C/power/Demo5Start 1: test_run1/4 Test #1: test_run ......................... Passed 0.00 secStart 2: test_5_22/4 Test #2: test_5_2 ......................... Passed 0.00 secStart 3: test_10_53/4 Test #3: test_10_5 ........................ Passed 0.00 secStart 4: test_2_104/4 Test #4: test_2_10 ........................ Passed 0.00 sec100% tests passed, 0 tests failed out of 4Total Test time (real) = 0.01 sec
如果要测试更多的输入数据,像上面那样一个个写测试用例未免太繁琐。这时可以通过编写宏来实现:
1234567891011
# 定义一个宏,用来简化测试工作macro (do_test arg1 arg2 result)add_test (test_${arg1}_${arg2} Demo ${arg1} ${arg2})set_tests_properties (test_${arg1}_${arg2}PROPERTIES PASS_REGULAR_EXPRESSION ${result})endmacro (do_test)# 使用该宏进行一系列的数据测试do_test (5 2 "is 25")do_test (10 5 "is 100000")do_test (2 10 "is 1024")
关于 CTest 的更详细的用法可以通过 man 1 ctest
参考 CTest 的文档。
支持 gdb
让 CMake 支持 gdb 的设置也很容易,只需要指定 Debug
模式下开启 -g
选项:
123
set(CMAKE_BUILD_TYPE "Debug")set(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g -ggdb")set(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")
之后可以直接对生成的程序使用 gdb 来调试。
添加环境检查
本节对应的源代码所在目录:Demo6。
有时候可能要对系统环境做点检查,例如要使用一个平台相关的特性的时候。在这个例子中,我们检查系统是否自带 pow 函数。如果带有 pow 函数,就使用它;否则使用我们定义的 power 函数。
添加 CheckFunctionExists 宏
首先在顶层 CMakeLists 文件中添加 CheckFunctionExists.cmake 宏,并调用 check_function_exists
命令测试链接器是否能够在链接阶段找到 pow
函数。
123
# 检查系统是否支持 pow 函数include (${CMAKE_ROOT}/Modules/CheckFunctionExists.cmake)check_function_exists (pow HAVE_POW)
将上面这段代码放在 configure_file
命令前。
预定义相关宏变量
接下来修改 config.h.in 文件,预定义相关的宏变量。
12
// does the platform provide pow function?#cmakedefine HAVE_POW
在代码中使用宏和函数
最后一步是修改 main.cc ,在代码中使用宏和函数:
1234567
#ifdef HAVE_POWprintf("Now we use the standard library. \n");double result = pow(base, exponent);#elseprintf("Now we use our own Math library. \n");double result = power(base, exponent);#endif
添加版本号
本节对应的源代码所在目录:Demo7。
给项目添加和维护版本号是一个好习惯,这样有利于用户了解每个版本的维护情况,并及时了解当前所用的版本是否过时,或是否可能出现不兼容的情况。
首先修改顶层 CMakeLists 文件,在 project
命令之后加入如下两行:
12
set (Demo_VERSION_MAJOR 1)set (Demo_VERSION_MINOR 0)
分别指定当前的项目的主版本号和副版本号。
之后,为了在代码中获取版本信息,我们可以修改 config.h.in 文件,添加两个预定义变量:
123
// the configured options and settings for Tutorial#define Demo_VERSION_MAJOR @Demo_VERSION_MAJOR@#define Demo_VERSION_MINOR @Demo_VERSION_MINOR@
这样就可以直接在代码中打印版本信息了:
12345678910111213141516171819202122232425262728293031
#include <stdio.h>#include <stdlib.h>#include <math.h>#include "config.h"#include "math/MathFunctions.h"int main(int argc, char *argv[]){if (argc < 3){// print version infoprintf("%s Version %d.%d\n",argv[0],Demo_VERSION_MAJOR,Demo_VERSION_MINOR);printf("Usage: %s base exponent \n", argv[0]);return 1;}double base = atof(argv[1]);int exponent = atoi(argv[2]);#if defined (HAVE_POW)printf("Now we use the standard library. \n");double result = pow(base, exponent);#elseprintf("Now we use our own Math library. \n");double result = power(base, exponent);#endifprintf("%g ^ %d is %g\n", base, exponent, result);return 0;}
生成安装包
本节对应的源代码所在目录:Demo8。
本节将学习如何配置生成各种平台上的安装包,包括二进制安装包和源码安装包。为了完成这个任务,我们需要用到 CPack ,它同样也是由 CMake 提供的一个工具,专门用于打包。
首先在顶层的 CMakeLists.txt 文件尾部添加下面几行:
1234567
# 构建一个 CPack 安装包include (InstallRequiredSystemLibraries)set (CPACK_RESOURCE_FILE_LICENSE"${CMAKE_CURRENT_SOURCE_DIR}/License.txt")set (CPACK_PACKAGE_VERSION_MAJOR "${Demo_VERSION_MAJOR}")set (CPACK_PACKAGE_VERSION_MINOR "${Demo_VERSION_MINOR}")include (CPack)
上面的代码做了以下几个工作:
- 导入 InstallRequiredSystemLibraries 模块,以便之后导入 CPack 模块;
- 设置一些 CPack 相关变量,包括版权信息和版本信息,其中版本信息用了上一节定义的版本号;
- 导入 CPack 模块。
接下来的工作是像往常一样构建工程,并执行 cpack
命令。
- 生成二进制安装包:
1
cpack -C CPackConfig.cmake
- 生成源码安装包
1
cpack -C CPackSourceConfig.cmake
我们可以试一下。在生成项目后,执行 cpack -C CPackConfig.cmake
命令:
12345678910111213141516171819
[ehome@xman Demo8]$ cpack -C CPackSourceConfig.cmakeCPack: Create package using STGZCPack: Install projectsCPack: - Run preinstall target for: Demo8CPack: - Install project: Demo8CPack: Create packageCPack: - package: /home/ehome/Documents/programming/C/power/Demo8/Demo8-1.0.1-Linux.sh generated.CPack: Create package using TGZCPack: Install projectsCPack: - Run preinstall target for: Demo8CPack: - Install project: Demo8CPack: Create packageCPack: - package: /home/ehome/Documents/programming/C/power/Demo8/Demo8-1.0.1-Linux.tar.gz generated.CPack: Create package using TZCPack: Install projectsCPack: - Run preinstall target for: Demo8CPack: - Install project: Demo8CPack: Create packageCPack: - package: /home/ehome/Documents/programming/C/power/Demo8/Demo8-1.0.1-Linux.tar.Z generated.
此时会在该目录下创建 3 个不同格式的二进制包文件:
12
[ehome@xman Demo8]$ ls Demo8-*Demo8-1.0.1-Linux.sh Demo8-1.0.1-Linux.tar.gz Demo8-1.0.1-Linux.tar.Z
这 3 个二进制包文件所包含的内容是完全相同的。我们可以执行其中一个。此时会出现一个由 CPack 自动生成的交互式安装界面:
12345678910111213141516171819202122232425262728293031323334353637383940
[ehome@xman Demo8]$ sh Demo8-1.0.1-Linux.shDemo8 Installer Version: 1.0.1, Copyright (c) HumanityThis is a self-extracting archive.The archive will be extracted to: /home/ehome/Documents/programming/C/power/Demo8If you want to stop extracting, please press <ctrl-C>.The MIT License (MIT)Copyright (c) 2013 Joseph Pan(http://hahack.com)Permission is hereby granted, free of charge, to any person obtaining a copy ofthis software and associated documentation files (the "Software"), to deal inthe Software without restriction, including without limitation the rights touse, copy, modify, merge, publish, distribute, sublicense, and/or sell copies ofthe Software, and to permit persons to whom the Software is furnished to do so,subject to the following conditions:The above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESSFOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS ORCOPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHERIN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR INCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.Do you accept the license? [yN]:yBy default the Demo8 will be installed in:"/home/ehome/Documents/programming/C/power/Demo8/Demo8-1.0.1-Linux"Do you want to include the subdirectory Demo8-1.0.1-Linux?Saying no will install in: "/home/ehome/Documents/programming/C/power/Demo8" [Yn]:yUsing target directory: /home/ehome/Documents/programming/C/power/Demo8/Demo8-1.0.1-LinuxExtracting, please wait...Unpacking finished successfully
完成后提示安装到了 Demo8-1.0.1-Linux 子目录中,我们可以进去执行该程序:
123
[ehome@xman Demo8]$ ./Demo8-1.0.1-Linux/bin/Demo 5 2Now we use our own Math library.5 ^ 2 is 25
关于 CPack 的更详细的用法可以通过 man 1 cpack
参考 CPack 的文档。
将其他平台的项目迁移到 CMake
CMake 可以很轻松地构建出在适合各个平台执行的工程环境。而如果当前的工程环境不是 CMake ,而是基于某个特定的平台,是否可以迁移到 CMake 呢?答案是可能的。下面针对几个常用的平台,列出了它们对应的迁移方案。
autotools
- am2cmake 可以将 autotools 系的项目转换到 CMake,这个工具的一个成功案例是 KDE 。
- Alternative Automake2CMake 可以转换使用 automake 的 KDevelop 工程项目。
- Converting autoconf tests
qmake
- qmake converter 可以转换使用 QT 的 qmake 的工程。
Visual Studio
- vcproj2cmake.rb 可以根据 Visual Studio 的工程文件(后缀名是
.vcproj
或.vcxproj
)生成 CMakeLists.txt 文件。 - vcproj2cmake.ps1 vcproj2cmake 的 PowerShell 版本。
- folders4cmake 根据 Visual Studio 项目文件生成相应的 “source_group” 信息,这些信息可以很方便的在 CMake 脚本中使用。支持 Visual Studio 9/10 工程文件。
CMakeLists.txt 自动推导
- gencmake 根据现有文件推导 CMakeLists.txt 文件。
- CMakeListGenerator 应用一套文件和目录分析创建出完整的 CMakeLists.txt 文件。仅支持 Win32 平台。
相关链接
- 官方主页
- 官方文档
- 官方教程
- Wiki
- FAQ
- bug tracker
- 邮件列表:
- cmake on Gmane
- http://www.mail-archive.com/cmake@cmake.org/
- http://marc.info/?l=cmake
- 其他推荐文章
- 在 linux 下使用 CMake 构建应用程序
- cmake的一些小经验
- Packaging Software with CPack
- 视频教程: 《Getting Started with CMake》
类似工具
- SCons:Eric S. Raymond、Timothee Besset、Zed A. Shaw 等大神力荐的项目架构工具。和 CMake 的最大区别是使用 Python 作为执行脚本。
这个页面详细罗列了使用 CMake 的知名项目↩
- CMake 入门实战(下)
- CMake 入门实战(上)
- CMake 入门实战(精)
- CMake 入门实战
- CMake 入门实战
- CMake 入门实战
- CMake入门实战
- CMake 入门实战
- Cmake入门实战
- CMake 入门实战
- CMake 入门实战
- CMake 入门实战
- CMake 入门实战
- CMake 入门实战(转)
- CMake 入门实战
- CMake 入门实战
- CMake入门学习+实战<一>
- CMake入门实战 补充说明
- Windows窗口游戏编程模板
- Android Drawable Resource学习(六)、LevelListDrawable
- Unity导出切割后的Sprite图片
- css如何实现背景透明,文字不透明?
- 如何拥有持久的激情?
- CMake 入门实战(下)
- HDU——2013 蟠桃记
- linux - restart apache出现ServerName
- 对象是从xib/storybard加载的时候,就会调用这个方法initWithCoder:(NSCoder *)aDecoder
- spring mvc 的Controller类是单例?
- 使用ngrok 将本地项目发布到公网,用于微信公众平台通过80端口访问本机
- Nim Game
- 自定义渲染器
- Java was started but returned exit code=13