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:
$ 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.
A normal version number MUST take the form X.Y.Z where X, Y, and Z are non-negative integers, and MUST NOT contain leading zeroes. X is the major version, Y is the minor version, and Z is the patch version. Each element MUST increase numerically. For instance: 1.9.0 -> 1.10.0 -> 1.11.0.
A version number is a unique sequence of numbers which identifies the state of development of computer software.
Version numbers are usually divided into sets of numbers, separated by decimal points. Typically, a change in the leftmost number indicates a major change in the software or driver. Changes in the rightmost number often indicate a minor change. Changes in other numbers represent varying degrees of changes.
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).
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.
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.
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).
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With