ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

CMake #2 添加版本号和配置的头文件

2021-10-29 23:30:02  阅读:370  来源: 互联网

标签:PROJECT CMake 版本号 project VERSION file 头文件 include Tutorial


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-level CMakeLists.txt also stores the project name in the variable CMAKE_PROJECT_NAME.

设置项目的名称,同时把它储存在变量PROJECT_NAME

Also sets the variables:

同时也设置了一些变量:

  • 项目源文件夹的绝对地址
  • 项目二进制文件夹绝对地址
  • 是否项目是最高版本的布尔值

更多的变量可以被以下可选的参数描述,如果这些参数没有被使用,那么相关的变量会被设置为空字符串

接着,配置一个头文件来将版本号传递给源文件

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 as add_executable() or add_library()

指定include文件夹用于编译一个给定的目标,目标必须被addadd_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;
    }
}

标签:PROJECT,CMake,版本号,project,VERSION,file,头文件,include,Tutorial
来源: https://blog.csdn.net/m0_61894296/article/details/121044771

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有