21

I have:

  1. C++ code with lots of complicated MACROS (#define bla bla ...)
  2. And the usage (instantiation) of those MACROS

I need:

A tool (online for example) that will simply do the instantiation of the MACROS (or the system of macros) and show the resultant code.

Example:

Input:

#define AAA(a,b) if ((a) > (b))

AAA(1, f1(10))

Output:

if ((1) > (f1(10)))
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Narek
  • 38,779
  • 79
  • 233
  • 389
  • possible duplicate of [C/C++ source file after preprocessing](http://stackoverflow.com/questions/277258/c-c-source-file-after-preprocessing) – Luchian Grigore Sep 19 '12 at 13:06
  • 1
    Use your **compiler**: For example, in GCC you can say, `gcc -E -P file.c`. – Kerrek SB Sep 19 '12 at 13:07
  • 5
    Why close vote? This is a programming related question! – Narek Sep 19 '12 at 13:07
  • Compilers have this built in. Which one are you using? Also, this is a terrible macro for C++; it should be an inline function like almost all macros. – tenfour Sep 19 '12 at 13:07
  • I work on Windows with MVS, and I dont want to see my file after preprocessing because it would be big and with lots of unnecessary information!!! – Narek Sep 19 '12 at 13:08
  • The close vote is for a duplicate question. – Luchian Grigore Sep 19 '12 at 13:08
  • 2
    It is not duplicate! I have already mentioned the difference with the one that you mentioned Luchian Grigore. – Narek Sep 19 '12 at 13:09
  • @Narek then you should specify that in the question. Edit it and clarify that it's not a duplicate because you only want the macro expansion of the current file, not the full preprocessing that will also expand `#include` directives. – Analog File Sep 19 '12 at 13:14
  • 1
    @KerrekSB sometimes you have reasons to use C preprocessor and you do not have any C compiler installed, like when working with GLSL (https://stackoverflow.com/questions/22496874/is-there-a-tool-to-get-preprocessed-output-of-a-glsl-shader/69140739#69140739) – Suma Sep 11 '21 at 06:57

1 Answers1

15

The gcc option would be -E and then your filename.

With the MSVC cl.exe the solution is also /E.

Marcus Riemer
  • 7,244
  • 8
  • 51
  • 76
  • What about not precompiling the whole file or project but a code snippet only? And what about doing this with Visual studio at least. – Narek Sep 19 '12 at 13:10
  • Added that, and sorry for the typo, for a second or two I gave the option as `-e`. – Marcus Riemer Sep 19 '12 at 13:13
  • Thanks, but still I deal with a very big project, and I need to prcompile only a small snippet. No faster way? – Narek Sep 19 '12 at 13:15
  • Not one that I would be aware of. – Marcus Riemer Sep 19 '12 at 13:17
  • @Narek Copy snippet to an empty tmp.c file, then run `gcc -E tmp.c` on that. – elomage Oct 08 '14 at 12:16
  • 24
    Good answer. Would like to add, if you want it online just go to http://coliru.stacked-crooked.com/ add `-E` and remove `&& a.out` in the command line. – Predelnik Jul 24 '15 at 16:09
  • 2
    @Predelnik Thank you for being the only person who actually answered the OP's question instead of giving a "don't do it" answer like everyone else did. – Sonic Atom Feb 03 '16 at 10:13