Question

I want to pass a version string in the compile command:

$ g++ -Wall -D VERSION="2013-12-03 02:15:21, commit cb060df" -o main main.cpp

Inside my code, I have the following:

#define TOSTR_(x) #x
#define STRINGIFY(x) TOSTR_(x)
#define VERSION_STR STRINGIFY(VERSION)

This doesn't work, because the VERSION macro has a comma in it, so it looks like I'm passing two arguments to TOSTR() (apparently, the VERSION macro only gets expanded after it's passed to STRINGIFY() as one unique argument).

The following approach I found in here also doesn't work:

#define CONCAT(...) __VA_ARGS__
#define TOSTR_(x) #x
#define STRINGIFY(x) TOSTR_(CONCAT(x))
#define VERSION_STR STRINGIFY(VERSION)

bacause this appears to be the same as

#define VERSIONSTR "CONCAT(2013-12-03 02:15:21, commit cb060df)"

that is, the macro CONCAT() doesn't get expanded.

Note 1: I would rather not pass a C string in the command line, because the version string is actually dynamically generated, and it might contain some quotes. This means that just writing g++ -D VERSION=\""$(GENERATED_STRING)"\" instead of stringifying the passed argument wouldn't work.

Note 2: I would be extremely happy if someone found a way of doing this without any preprocessor macros at all.

Was it helpful?

Solution

You can write the TOSTR_ macro to take variable arguments:

#define TOSTR_(x...) #x
#define STRINGIFY(x) TOSTR_(x)
#define VERSION_STR STRINGIFY(VERSION)

This code has been tested and works on Apple LLVM version 5.0.

OTHER TIPS

__VA_ARGS__ must be in each call:

#define _STRINGIZE(...) #__VA_ARGS__
#define STRINGIZE(...) _STRINGIZE(__VA_ARGS__)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top