其他分享
首页 > 其他分享> > CMake #2 添加版本号和配置的头文件

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-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