I need to be override certain macro definition by my header file. And I am not allowed to change source code. And I have to use gcc, but if anyone is aware of something similar on any other compiler then also it will help.
Here is what I exactly need:
Lets say I have code base with lot of .c files. These .c files include .h files. After all the .h files have been included for each file I want the compiler to behave as if I have another extra.h
file which I want to specify when invoking the compiler. What I do in that .h file is #undef
some macro and re-define the macro the way I want them to be.
Note: I am aware of --preinclude
option in gcc, but using --preinclude
over-rides my extra.h by the .h of the original source code. What I need is some kind of post include option.
Unless you uniformly have one specific header that is always included last in the source files, this is going to be tricky.
I think the way I'd approach it, if I had to, would be:
headers
.#include "extra.h"
at the end (or possibly #include <extra.h>
, but I would try to avoid that).#include "/usr/include/header.h"
but preferably some other technique - such as #include "include/header."
.extra.h
header would always redefine all its macros - it would not have the normal #ifndef EXTRA_H_INCLUDED
/ #define EXTRA_H_INCLUDED
/ #endif
multiple inclusion guards, so that each time it is included, it would redefine the relevant macros.extra.h
cannot define any types. (Or, more precisely, if it does, those must be protected against multiple definition by multiple include guards; the key point is that the macros must be defined each time the file is included - a bit like <assert.h>
.) #undef REDEFINED_MACRO
and then #define REDEFINED_MACRO ...
.headers
directory before looking anywhere else. The compiler option would be -I./headers
or something similar, depending on exactly where you locate the headers
directory.-I
option (such as -I/usr
if you've used #include "include/header.h"
notation) to locate the standard headers again.The upshot is that your private headers get used directly by the compiler, but they include the standard headers and then your extra.h
header - thus achieving what you wanted without modifying the C source or the normal headers.
But there is something misguided about the whole attempt...you would be better off not trying this.
You can use
-include file
option of GCC, because of this feature:
If multiple -include options are given, the files are included in the order they appear on the command line.
So as I understand you must include ALL *.h files from the command line,- just keep your "extra.h" the last header in -include
option list and you should get what you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With