2

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();.

Community
  • 1
  • 1
BalticMusicFan
  • 653
  • 1
  • 8
  • 21
  • Explain this in more defails - I have completely no idea what you're trying to achieve. The first thought is that you are probably trying to "overload" a macro, that is, you want that it do different things depending on what name you pass as 'name' to 'PROTOTYPENAMED' macro. How would you like to use the"second version"of that macro and what result would you like to achieve? – Ethouris Feb 04 '15 at 17:48
  • @Ethouris read an question again, macro overloading is unexpected behaviour. _I might want to write_ `#define PROTOTYPENAMED(ANOTHERFUNCTION) WhatsThat()` _to generate_ `#define ANOTHERFUNCTION_ABC WhatsThat()` – BalticMusicFan Feb 04 '15 at 18:16
  • So, yes, as Basile said, it's not possible to achieve in C/C++ preprocessor. This better be some completely generator-based thing. I personally use Tcl for that, not only because I like it, but that it can simply contain the whole text in {...} without any special "escapes". Then you can either contain $variables inside or have some unique-impossible-in-normal-program statmenents that will be next replaced by simple search-replace ("string map" command in Tcl) - such as @KEY@ used in *.in files. – Ethouris Feb 05 '15 at 17:58

1 Answers1

4

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.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547