English 中文(简体)
What s the best way to insert version and arch info into C++ sources?
原标题:

I want my C++ program to include a "--version" option which causes it to print out:

  • Architecture it s compiled for
  • Version of the source (e.g., v0.1.0)
  • Name of the application

I m also using autoconf/automake for the first time, and I notice that configure.ac has both the binary and the version. It doesn t currently have architecture information in it, and I don t want to add such info, since I ll be compiling under multiple arches.

Is there an easy way to automate the insertion of the version/arch/appname information in a header or source file? How do most C++ coders do this?

最佳回答

autoconf gives you a config.h, which provides string macros such as PACKAGE, PACKAGE_NAME, PACKAGE_STRING, PACKAGE_VERSION, and VERSION.

(When I use the Argp argument parser, I simply use

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <argp.h>
const char *argp_program_version = PACKAGE_STRING;
const char *argp_program_bug_address = PACKAGE_BUGREPORT;
/* et cetera */
int main(int argc, char **argv) {
    error_t argp_result = argp_parse(&argp, argc, argv, 0, 0, NULL);
    if (argp_result) return argp_result;
    /* et cetera */
}

and then --help, --version, etc. just automatically work.)

You could also add

AC_CANONICAL_HOST
AC_DEFINE_UNQUOTED([CHOST], ["$host"], [Canonical host])
AC_CANONICAL_BUILD
AC_DEFINE_UNQUOTED([CBUILD], ["$build"], [Canonical build])
AC_CANONICAL_TARGET
AC_DEFINE_UNQUOTED([CTARGET], ["$target"], [Canonical target])

to your configure.ac, if you want to have those string macros in your config.h too. These are typically strings of the form $arch-$vendor-$kernel-$libc, where
CHOST is the platform that will run the software after it is built,
CBUILD is the platform that is currently building the software, and
CTARGET is the platform the software will act on.
(These are all the same unless you are cross-compiling or building a cross-compile toolchain.)

问题回答

Write a python script that generate your version source file. Then make it run by whatever build scripts/tool you re using.

Choose a rule to actually rewrite the version file and enforce it in the python script : it s really dependent on the type of project and release organisation you re doing.


In my home project I use a script that generate a version file each time I build, because I need to know wich computer-developer built the version and at wich time.

But in a lot of job works I used a script that generated the version file but only if I wanted it to. That s because the dev team decided when a specific version was a "release".

I suggested using a Python script because Python runs on all developement platforms and is really powerful and easy when it comes to text files manipulations. It can then be called by whatever build system (Visual Studio, CMake, etc.) you use.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签