bash 在程序代码中编码/嵌入版本号的正确方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27395120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
correct way to encode/embed version number in program code
提问by Barracuda
I would like to embed my software version in my code and later retrieve it from the program binary with a command like argument like -v
or --version
. As an example some of the GNU/Linux software binaries print their version information when supplied with a -v
or -V
argument in the command line take ls
as an example:
我想将我的软件版本嵌入我的代码中,然后使用类似参数的命令从程序二进制文件中检索它-v
或--version
。例如,一些 GNU/Linux 软件二进制文件在命令行中提供-v
或-V
参数时打印其版本信息,ls
例如:
$ ls --version
ls (GNU coreutils) 8.13
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Is there a convention or standard do this? I tried to search for this information but since I didn't know the right terminology my search only resulted in version control howto's. The software code is written in C++ language if it helps to address the problem better.
是否有约定或标准来执行此操作?我试图搜索此信息,但由于我不知道正确的术语,因此我的搜索仅导致版本控制方法。如果有助于更好地解决问题,则软件代码是用 C++ 语言编写的。
回答by Mikael Persson
Normally, for major and minor version numbers (as in, 1.2, 1 is major and 2 is minor), they are most often written in the code directly, usually as a #define
(because you might need them for conditional compilations, i.e., #if
blocks).
通常,对于主要和次要版本号(如 1.2,1 是主要版本,2 是次要版本),它们通常直接写在代码中,通常作为 a #define
(因为您可能需要它们进行条件编译,即#if
块) .
You would typically have a separate header that contains only those defines and nothing else (except the header-guard), to minimize dependencies.
您通常会有一个单独的标头,其中仅包含那些定义而没有其他任何内容(标头保护除外),以最大程度地减少依赖性。
Some people use the build system (like cmake) to pull a version number from the version control (git, svn, cvs, etc..) and then put that version number into their "version" header. Or, they put the version number into the build system configuration files and then put that onto the header, as shown on the cmake tutorial. Personally, I don't like this approach because it tends to modify your header files too often and cause frequent and pointless recompilations.
有些人使用构建系统(如 cmake)从版本控制(git、svn、cvs 等)中提取版本号,然后将该版本号放入他们的“版本”标题中。或者,他们将版本号放入构建系统配置文件中,然后将其放入标题中,如cmake 教程中所示。就我个人而言,我不喜欢这种方法,因为它往往会过于频繁地修改您的头文件并导致频繁且毫无意义的重新编译。
I prefer writing the version number in the header file, and then pulling out those version numbers (major, minor, ..) from the header onto the build system. This is another thing that cmake can very easily do.
我更喜欢在头文件中写入版本号,然后从头文件中提取这些版本号(主要、次要、..)到构建系统。这是 cmake 可以很容易地做到的另一件事。
If you want to embed a very day-by-day version number into your software, such as a build number or revision number, then you should not put it as a #define
in a header file, but rather as a extern const
variable that you define in one cpp file. For example, you can use cmake to pull a revision number from your version control system, tack that onto the cpp file that defines this extern const int revision;
variable (through cmake's configure_file
function), and link your programs with that cpp / object file. This way, the revision number is built into your programs automatically at every re-build, and it won't trigger full recompilations every time it's updated (which is at every commit).
如果你想在你的软件中嵌入一个非常日常的版本号,比如内部版本号或修订号,那么你不应该把它作为#define
一个头文件,而是作为一个extern const
你定义的变量.cpp 文件。例如,您可以使用 cmake 从您的版本控制系统中提取修订号,将其添加到定义此extern const int revision;
变量的 cpp 文件上(通过 cmake 的configure_file
函数),并将您的程序与该 cpp / 目标文件链接起来。这样,每次重新构建时,修订号都会自动内置到您的程序中,并且不会在每次更新时(每次提交时)都触发完全重新编译。
The point is that major and minor version numbers are not changed frequently enough to require any kind of automatic maintenance, but you need to manually write them in one place only, and automatically propagate it everywhere else where it might be relevant (I would recommend that this place be the header file itself). It's only the revision or build numbers that need to be fully automated (generated by the version control, and propagated everywhere else automatically).
关键是主要和次要版本号的更改频率不足以要求任何类型的自动维护,但是您只需要在一个地方手动编写它们,并在可能相关的其他地方自动传播它(我建议这个地方是头文件本身)。只有修订或内部版本号需要完全自动化(由版本控制生成,并自动传播到其他任何地方)。
回答by Gábor Angyal
I believe it is customary to keep the version number in a dedicated header file. Some tools can automatically generate this for you. For example here is how you do it with CMake: https://cmake.org/cmake/help/latest/guide/tutorial/index.html
我相信习惯上将版本号保存在一个专用的头文件中。一些工具可以自动为你生成这个。例如,这里是您如何使用 CMake:https: //cmake.org/cmake/help/latest/guide/tutorial/index.html
See the section "Adding a Version Number and Configured Header File"
请参阅“添加版本号和配置的头文件”部分