Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding macros using compiler options

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.

like image 709
Saurabh Avatar asked Oct 17 '25 15:10

Saurabh


2 Answers

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:

  • Create a new directory, call it headers.
  • Put in there suitable dummy headers with the same name as the regular headers, which would contain #include "extra.h" at the end (or possibly #include <extra.h>, but I would try to avoid that).
  • The dummy headers would also include the original files by some mechanism, possibly even using #include "/usr/include/header.h" but preferably some other technique - such as #include "include/header.".
  • The 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.
  • Consequently, 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>.)
  • Each redefined macro would be explicitly protected by #undef REDEFINED_MACRO and then #define REDEFINED_MACRO ....
  • There is no point in testing whether the macro is defined before undefining it.
  • The build process would be modified to look in the 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.
  • Depending on how you have decided to locate the normal versions of the headers, you might need another -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.

like image 179
Jonathan Leffler Avatar answered Oct 21 '25 00:10

Jonathan Leffler


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.

like image 36
Agnius Vasiliauskas Avatar answered Oct 20 '25 23:10

Agnius Vasiliauskas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!