Reputation: 443
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
#define SETTING1
#define header1_h
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);
}
Upvotes: 2
Views: 1317
Reputation: 52294
gcc
has an option -fdirectives only
which does something close to what you want:
-fdirectives-only
When preprocessing, handle directives, but do not expand macros.
The option’s behavior depends on the -E and -fpreprocessed options.
With -E, preprocessing is limited to the handling of directives such as #define, #ifdef, and #error. Other preprocessor operations, such as macro expansion and trigraph conversion are not performed. In addition, the -dD option is implicitly enabled.
With -fpreprocessed, predefinition of command line and most builtin macros is disabled. Macros such as
__LINE__
, which are contextually dependent, are handled normally. This enables compilation of files previously preprocessed with -E -fdirectives-only.With both -E and -fpreprocessed, the rules for -fpreprocessed take precedence. This enables full preprocessing of files previously preprocessed with -E -fdirectives-only.
In your case, it should be called
% gcc -fdirectives-only -E -o main.i main.c
but you get more defines (those internally defined), blank lines and #line
lines than what you ask for.
Upvotes: 2