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.hheader would always redefine all its macros – it would not have the normal#ifndef EXTRA_H_INCLUDED/#define EXTRA_H_INCLUDED/#endifmultiple inclusion guards, so that each time it is included, it would redefine the relevant macros.extra.hcannot 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_MACROand then#define REDEFINED_MACRO ....headersdirectory before looking anywhere else. The compiler option would be-I./headersor something similar, depending on exactly where you locate theheadersdirectory.-Ioption (such as-I/usrif 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.hheader – 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.