1

I'm trying to review a C/C++ project which is heavily dealing with macros and function-like macros. What I would like to do is to replace the define and function-like macros with their replacement.

For example I have this file:

#include <iostream>

#define SUM(a,b,c,d) a+b+c+d
using namespace std;

int main(){

    cout << SUM(1,2,3,4) << endl;

}

And I want to reach to this file:

#include <iostream>

using namespace std;

int main(){

    cout << 1+2+3+4 << endl;

}

Please note that I'm not looking to replace the #include lines.

EDIT:

gcc -E expands the #define macros but it will also expand the #include macros as well. I DO NOT want the #include to be expanded.

Omid N
  • 947
  • 2
  • 11
  • 24
  • Maybe this will help: [Preprocessor output](https://stackoverflow.com/questions/3742822/preprocessor-output) – Fiddling Bits May 04 '20 at 13:32
  • 3
    @FiddlingBits It won't directly help this case because it will also expand `#include `. – MikeCAT May 04 '20 at 13:32
  • related/dupe: https://stackoverflow.com/questions/3019609/any-utility-to-test-expand-c-c-define-macros – NathanOliver May 04 '20 at 13:33
  • 4
    @MikeCAT you could first remove all lines with an include, then "compile" it – RoQuOTriX May 04 '20 at 13:33
  • Does this answer your question? [How do I run the GCC preprocessor to get the code after macros like #define are expanded?](https://stackoverflow.com/questions/3916979/how-do-i-run-the-gcc-preprocessor-to-get-the-code-after-macros-like-define-are) – Marco Bonelli May 04 '20 at 13:34
  • Which compiler are you using? – Aykhan Hagverdili May 04 '20 at 13:34
  • Does this answer your question? [Seeing expanded C macros](https://stackoverflow.com/questions/985403/seeing-expanded-c-macros) – Marek R May 04 '20 at 13:36
  • Just saying, Clangd (e.g. when used with VSCode) is able expand macros on demand. (Not in entire files, but separate macro invocations.) – HolyBlackCat May 04 '20 at 13:37
  • 1
    The duplicates proposed so far are not correct, because this question asks to expand the macros defined with `#define` but not to process `#include` statements. I do not think GCC has switches for that. One might do it by creating a copy of the source file with `#include` lines altered to suppress them, then using GCC, then restoring the `#include` lines. – Eric Postpischil May 04 '20 at 13:48
  • Note that processing the `#define` lines alone will not necessarily produce an equivalent source file—doing macro replacement first, then including included files, then doing macro replacement again may have different results than including included files and doing macro replacement once. – Eric Postpischil May 04 '20 at 13:50
  • 1
    untested -- I think I used this before -- `gcc -no-stdinc -E ...` *or something similar* – pmg May 05 '20 at 09:05
  • @pmg `g++ -nostdinc -E ...` worked for me. Looks like it forgets the include path which will result in an include failure (It gives an include error) but it will expand the `#define` macros which is what I wanted. Please write an answer so I can mark it. – Omid N May 05 '20 at 09:30

3 Answers3

3

I've used gcc -nostdinc -E ... (or something similar) before.

Can't test this at the moment; don't really remember exactly why I used it either

pmg
  • 106,608
  • 13
  • 126
  • 198
2

Use gcc -E to preprocess your source file. Then find the first line of the original source file after all #include and remove all lines before that first line. Then restore #include from the original source file.

Preprocessor marks original source lines for debugger like below so you can easily find the proper original first line after #include.

# 1 "main.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 31 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 32 "<command-line>" 2
# 1 "main.c"

What do the numbers mean in the preprocessed .i files when compiling C with gcc?

273K
  • 29,503
  • 10
  • 41
  • 64
0

GCC has the -E flag which makes it output preprocessed source code.

Pat Pac
  • 27
  • 3
  • 3
    This is incorrect because `gcc -E` does the full preprocessing, including `#define` and `#include` directives, but the question asks not to process `#include` directives. – Eric Postpischil May 04 '20 at 13:49
  • I agree. I've misunderstood. The -E flag might still be useful though if You don't have to go through tones of includes appended in the output. I don't believe there's any ready solution like that for what OP needs. – Pat Pac May 04 '20 at 14:01