I’m building a large C++ program with a variety of different compile-time options, selected by #defines (or the -D option).
I want to have a version string that lists a number of them as tags, and ideally, to have that version string defined as a literal, not a constant.
Currently, I’m looking at three options, none of which is ideal.
-
Piles of preprocessor defines
#ifdef AAA #define AAAMSG " [A]" #else #define AAAMSG "" #endif #ifdef BBB #define BBBMSG " [B]" #else #define BBBMSG "" #endif // ... #define REVISION __DATE__ " " __TIME__ AAAMSG BBBMSG CCCMSG DDDMSG -
Build a constant
const char *const REVISION=__DATE__ " " __TIME__ #ifdef AAA " [A]" #endif #ifdef BBB " [B]" #endif // ... ; -
Redefine the token
#define REVISION __DATE__ " " __TIME__ #ifdef AAA #define REVISION REVISION " [A]" #endif #ifdef BBB #define REVISION REVISION " [B]" #endif // ...
The first one is incredibly verbose (imagine that with half a dozen independent elements) and error-prone. The second one is far better, but it creates a constant instead of a literal, so I can’t use it as part of another string – example:
send(sock,"rev " REVISION "\n",sizeof(REVISION)+4,0);
It seems silly to use run-time string manipulation (an sprintf or somesuch) for a compile-time constant. The third example, of course, just straight-up doesn’t work, but it is pretty much what I’m trying to do.
Is there some alternative method?
Closing off this question with the comment that I’m sticking with option 1. There appears to be no way to do what I was hoping to do, so the imperfect remains. Thanks to those who contributed!