当前位置: 动力学知识库 > 问答 > 编程问答 >

c++ - How to stringify a string which contains a comma?

问题描述:

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.

网友答案:

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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: