如何在C中串联两个字符串宏? [英] How do I concatenate two string macros in C?

查看:40
本文介绍了如何在C中串联两个字符串宏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的程序实现VERSION宏,在某些情况下需要更改该宏.

宏版本是通过Makefile定义的(git info放在此处),它是一个字符串.现在,我有一组#define开关,并且我希望VERSION反映其中的哪个开关.现在看起来像下面的内容(main.h):

  #define COMPLEX_DEPOSITION//这是开关.稍后在代码中将其用于#ifdef ...#endif构造.#ifdef COMPLEX_DEPOSITION#define CD"_COMP_DEP"//这是我要放在VERSION末尾的字符串#define VERSION_ VERSION CD#undef VERSION////禁止显示宏重新定义"警告#define VERSION VERSION_#undef VERSION_#万一 

好吧,我遇到了很多错误,其中大多数使我认为C预处理程序可以按随机顺序处理文件中的行:(

后来我有一个更复杂的东西,目的是使 VERSION->VERSION_WLT_GAP_2

 <代码> #define WIRESLIFETIMES#ifdef娱乐时间#define GAP 2#define VERSION_(VERSION ##"_WLT_GAP_" ## #GAP)#define VERSION VERSION_#undef VERSION_#万一 

我不知道该怎么办,甚至不知道

解决方案

好吧,答案似乎是这样的:

 <代码>//https://stackoverflow.com/questions/5256313/c-c-macro-string-concatenation//将预处理器令牌A和B连接起来而不扩展宏定义(但是,如果从宏调用,则扩展宏参数).#定义PPCAT_NX(A,B)A ## B//在宏扩展后将预处理器令牌A和B连接起来.#定义PPCAT(A,B)PPCAT_NX(A,B)//在不扩展宏定义的情况下将A转换为字符串文字(但是,如果从宏调用,则将扩展宏参数).#定义STRINGIZE_NX(A)#A//将A宏展开后将其转换为字符串文字.#定义STR(A)STRINGIZE_NX(A)#定义COMPLEX_DEPOSITION#ifdef COMPLEX_DEPOSITION#定义CD"_COMPDEP"#别的#define CD"#万一#define WIRESLIFETIMES#ifdef娱乐时间#define GAP 2#定义WLT STR(PPCAT(_WLT:G,GAP))#定义DISABLE_METROPOLIS#别的#define WLT"#万一#define VERSION VERSIONX CD WLT 

产生 V008.1-11-g68a9c89cb4-dirty_COMPDEP_WLT:G2

,我对此感到满意.

必须注意,我在Makefile中将 -DVERSION = ... 更改为 -DVERSIONX = ...

I am trying to implement VERSION macro for my program, that is to be changed under certain circumstances.

macro VERSION is defined via Makefile (git info is put there) and is a string. Now I have a set of #define'd switches and I want VERSION to reflect which of them are on. This looks now like the follows (main.h):

#define COMPLEX_DEPOSITION // This is switch. later in code it is used in #ifdef...#endif construction.

#ifdef COMPLEX_DEPOSITION
#define CD "_COMP_DEP" // this is the string I want to put in the end of VERSION
#define VERSION_ VERSION CD

#undef VERSION // this is to suppress 'macro redefinition' warning
#define VERSION VERSION_
#undef VERSION_
#endif

Well, I get a lot of errors, most of which make me think that C preprocessor works with lines in file in random order:(

Later I have an even more complex thing that is intended to make VERSION -> VERSION_WLT_GAP_2

#define WIRESLIFETIMES

#ifdef WIRESLIFETIMES
#define GAP 2
#define VERSION_ (VERSION ## "_WLT_GAP_" ## #GAP)
#define VERSION VERSION_
#undef VERSION_
#endif

and I got no idea what to do and if this is even possible

解决方案

Well, the answer seems to be the following:

// https://stackoverflow.com/questions/5256313/c-c-macro-string-concatenation
// Concatenate preprocessor tokens A and B without expanding macro definitions (however, if invoked from a macro, macro arguments are expanded).
#define PPCAT_NX(A, B) A ## B

// Concatenate preprocessor tokens A and B after macro-expanding them.
#define PPCAT(A, B) PPCAT_NX(A, B)

// Turn A into a string literal without expanding macro definitions (however, if invoked from a macro, macro arguments are expanded).
#define STRINGIZE_NX(A) #A

// Turn A into a string literal after macro-expanding it.
#define STR(A) STRINGIZE_NX(A)


#define COMPLEX_DEPOSITION

#ifdef COMPLEX_DEPOSITION
#define CD "_COMPDEP"
#else
#define CD ""
#endif


#define WIRESLIFETIMES

#ifdef WIRESLIFETIMES
#define GAP 2
#define WLT STR(PPCAT(_WLT:G, GAP))
#define DISABLE_METROPOLIS
#else
#define WLT ""
#endif

#define VERSION VERSIONX CD WLT

which produces V008.1-11-g68a9c89cb4-dirty_COMPDEP_WLT:G2 and I am happy with it.

Must be noted that I changed -DVERSION=... to -DVERSIONX=... inside Makefile

这篇关于如何在C中串联两个字符串宏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆