CMake #2 添加版本号和配置的头文件
2021/10/29 23:42:02
本文主要是介绍CMake #2 添加版本号和配置的头文件,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
CMake #2 添加版本号和配置的头文件
https://cmake.org/cmake/help/v3.22/guide/tutorial/A%20Basic%20Starting%20Point.html#adding-a-version-number-and-configured-header-file
While we could do this exclusively in the source code, using
CMakeLists.txt
provides more flexibility.虽然我们可以单独在源代码中这么做,但是使用cmake可以更加灵活
首先,修改CMakeLists.txt,使用project()命令来设置项目名称和版本号
# set the project name and version project(Tutorial VERSION 1.0)
project命令:
Synopsis概要
project(<PROJECT-NAME> [<language-name>...]) project(<PROJECT-NAME> [VERSION <major>[.<minor>[.<patch>[.<tweak>]]]] [DESCRIPTION <project-description-string>] [HOMEPAGE_URL <url-string>] [LANGUAGES <language-name>...])Sets the name of the project, and stores it in the variable
PROJECT_NAME
. When called from the top-levelCMakeLists.txt
also stores the project name in the variableCMAKE_PROJECT_NAME
.设置项目的名称,同时把它储存在变量
PROJECT_NAME
里Also sets the variables:
PROJECT_SOURCE_DIR
,<PROJECT-NAME>_SOURCE_DIR
Absolute path to the source directory for the project.
PROJECT_BINARY_DIR
,<PROJECT-NAME>_BINARY_DIR
Absolute path to the binary directory for the project.
PROJECT_IS_TOP_LEVEL
,_IS_TOP_LEVEL
*New in version 3.21.*Boolean value indicating whether the project is top-level.
同时也设置了一些变量:
- 项目源文件夹的绝对地址
- 项目二进制文件夹绝对地址
- 是否项目是最高版本的布尔值
更多的变量可以被以下可选的参数描述,如果这些参数没有被使用,那么相关的变量会被设置为空字符串
接着,配置一个头文件来将版本号传递给源文件
configure_file(TutorialConfig.h.in TutorialConfig.h)
configure_file命令
Copy a file to another location and modify its contents.
复制一个文件到另外的位置,并且调整它的内容
configure_file(<input> <output> [NO_SOURCE_PERMISSIONS | USE_SOURCE_PERMISSIONS | FILE_PERMISSIONS <permissions>...] [COPYONLY] [ESCAPE_QUOTES] [@ONLY] [NEWLINE_STYLE [UNIX|DOS|WIN32|LF|CRLF] ])Copies an
<input>
file to an<output>
file and substitutes variable values referenced as@VAR@
or${VAR}
in the input file content.
因为配置文件会被写入Binary Tree,我们必须把该文件夹添加到include文件的搜索路径中,添加:
target_include_directories(Tutorial PUBLIC "${PROJECT_BINARY_DIR}")
Binary Tree:This is hierarchy of directories where CMake will store generated files and where native build tool will store it’s temporary files.
target_include_directories
Add include directories to a target.
给目标添加include文件夹
target_include_directories(<target> [SYSTEM] [AFTER|BEFORE] <INTERFACE|PUBLIC|PRIVATE> [items1...] [<INTERFACE|PUBLIC|PRIVATE> [items2...] ...])Specifies include directories to use when compiling a given target. The named
<target>
must have been created by a command such asadd_executable()
oradd_library()
指定include文件夹用于编译一个给定的目标,目标必须被add
add_executable()
或者add_library()
创建
新建TutorialConfig.h.in文件
// the configured options and settings for Tutorial #define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@ #define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@
当cmake配置这个头文件的值时,@Tutorial_VERSION_MAJOR@和@Tutorial_VERSION_MINOR@就会被替代
接下来修改源文件,包含TutorialConfig.h头文件
最后,输出可执行文件的名称和版本号
#include <iostream> #include "TutorialConfig.h" int main(int argc, char **argv) { if (argc < 2) { std::cout << argv[0] << "Version" << Tutorial_VERSION_MAJOR << "." << Tutorial_VERSION_MINOR << std::endl; } }
这篇关于CMake #2 添加版本号和配置的头文件的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23增量更新怎么做?-icode9专业技术文章分享
- 2024-11-23压缩包加密方案有哪些?-icode9专业技术文章分享
- 2024-11-23用shell怎么写一个开机时自动同步远程仓库的代码?-icode9专业技术文章分享
- 2024-11-23webman可以同步自己的仓库吗?-icode9专业技术文章分享
- 2024-11-23在 Webman 中怎么判断是否有某命令进程正在运行?-icode9专业技术文章分享
- 2024-11-23如何重置new Swiper?-icode9专业技术文章分享
- 2024-11-23oss直传有什么好处?-icode9专业技术文章分享
- 2024-11-23如何将oss直传封装成一个组件在其他页面调用时都可以使用?-icode9专业技术文章分享
- 2024-11-23怎么使用laravel 11在代码里获取路由列表?-icode9专业技术文章分享
- 2024-11-22怎么实现ansible playbook 备份代码中命名包含时间戳功能?-icode9专业技术文章分享