Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22461

Partial preprocessing of C files with GCC (not removing "define" directives)

$
0
0

GCC can output a fully preprocessed C++ source file if I pass the -E or -save-temps command line argument.

My question is, can I somehow get a partially preprocessed C++ source file in which

a) code fragments not meeting #if, #ifdef and #ifndef conditions are eliminated,

b) #include directives are resolved (header files are included), etc

BUT

c) ordinary #define directives are NOT resolved?

(This would be necessary and really helpful because I would like to have the most compact and readable output possible. Resolving the #if directives shortens the source code, but resolving #define directives makes the source less readable and more redundant.)

I have tried to create an example as compact as possible in order to demonstrate what I would like to achieve:

Sample input files:

// header1.h
#ifndef header1_h
#define header1_h
int function1(int val) {
  return val + MYCONST;
}
#endif

// header2.h
#ifndef header2_h
#define header2_h
int function1(int val) {
  return val + val + MYCONST;
}
#endif

// main.c
#define MYCONST 1234
#define SETTING1
#ifdef SETTING1
  #include "header1.h"
#endif
#ifdef SETTING2
  #include "header2.h"
#endif
int main(void) {
  int retVal = function1(99);
}

Expected output:

// main.i (GCC preprocessing output)
#define MYCONST 1234 // I would like to see the definition of MYCONST here
int function1(int val) {
  return val + MYCONST; // I would like to see MYCONST here instead of the resolved value
}
int main(void) {
  int retVal = function1(99);
}

Viewing all articles
Browse latest Browse all 22461

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>