开发者

Define a string with parts coming from other defines(non strings)

开发者 https://www.devze.com 2023-03-26 15:22 出处:网络
I\'m trying to create a version string, which is treated as a char* when used. I\'m currently using #define VERSION_MAJOR @cmakeproject_VERSION_MAJOR@

I'm trying to create a version string, which is treated as a char* when used. I'm currently using

#define VERSION_MAJOR @cmakeproject_VERSION_MAJOR@
#define VERSION_MINOR @cmakeproject_VERSION_MINOR@
#define VERSION_PATCH @cmakeproject_VERSION_PATCH@
#define VERSION_STRING "" VERSION_MAJOR "." VERSION_MINOR "." VERSION_PATCH

After cmake configures, this will look like

#define VERSION_MAJOR 1
#define VERSION_MINOR 2
#define VERSION_PATCH 3
#define VERSION_STRING "" VERSION_MAJOR "." VERSION_MINOR "." VERSION_PATCH

As far as I understand, VERSION_STRING at compile-time will end up looking like

"" 1 "." 2 "." 3

And, this results in error: expected ‘)’ before numeric c开发者_Python百科onstant

Is there a way to make it so that VERSION_STRING at compile time looks like "1.2.3"?


Stringification with slingshot should do the trick:

#define QU(x) #x
#define QUH(x) QU(x)
#define VERSION_STRING QUH(VERSION_MAJOR) "." QUH(VERSION_MINOR) "." QUH(VERSION_PATCH)

It'll expand to "1" "." "2" "." "3", which is the same as "1.2.3".

0

精彩评论

暂无评论...
验证码 换一张
取 消