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.

12
  • Maybe this will help: Preprocessor output Commented May 4, 2020 at 13:32
  • 3
    @FiddlingBits It won't directly help this case because it will also expand #include <iostream>. Commented May 4, 2020 at 13:32
  • 4
    @MikeCAT you could first remove all lines with an include, then "compile" it Commented May 4, 2020 at 13:33
  • 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. Commented May 4, 2020 at 13:48
  • 1
    untested -- I think I used this before -- gcc -no-stdinc -E ... or something similar Commented May 5, 2020 at 9:05

3 Answers 3

4

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

Sign up to request clarification or add additional context in comments.

Comments

3

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?

Comments

1

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

2 Comments

This is incorrect because gcc -E does the full preprocessing, including #define and #include directives, but the question asks not to process #include directives.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.