generate a name for macro with another macro (c preprocessor) [duplicate]

馋奶兔 提交于 2021-01-28 12:18:54

问题


I can generate name for a function using an macro which is taken from C pre-processor defining for generated function names .

#define POSTFIX _ABC //_ABC should be mentioned only there
#define PROTOTYPENAME(symbol) symbol ## POSTFIX
#define PROTOTYPENAMED(sym) PROTOTYPENAME(sym)

int PROTOTYPENAMED(Function)(int a, int b, int c);

Above would result as

int Function_ABC(int a, int b, int c);

If I want to generate equivalent for #define ANOTHERFUNCTION_ABC WhatsThat() I might want to write

#define PROTOTYPENAMED(ANOTHERFUNCTION) WhatsThat()

but this will redefine a PROTOTYPENAMED macro. Is there a way to do it correctly without writing _ABC everywhere?

PS. I am using Visual Studio 2013 and code should work in C and C++.

Edit: alk: this question contains more useful answer how to do this using another tools. You should not mark this question as dublicate.

Edit 2: I was thinking about alternatives that do not need an external tools. Maybe it is not needed to declare it as macro? If we assume WhatsThat() is returning int, it is not forbidden to write int PROTOTYPENAMED(ANOTHERFUNCTION) = WhatsThat();.


回答1:


Not doable with the standard C/C++ preprocessor. However, you might feed your compiler using some C++ or C source generator, like e.g. the GPP or the GNU m4 preprocessors, or some script -e.g. in Python, awk, guile, ...- (or program) of yours.

For example, yacc or bison and ANTLR generate C++ code. And MELT is compiled into C++ code.

Of course, you need to configure your builder accordingly, e.g. add some more GNU make rules.



来源:https://stackoverflow.com/questions/28327691/generate-a-name-for-macro-with-another-macro-c-preprocessor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!