Reputation: 4699
Let's say that I have some code as follows:
L - A common library (source) (via svn:extern)
X - My project
Y - A static library (source) (via svn:extern) compiled with X and depends on L
Inside of L
there is a macro that is extensively in code in X
and Y
#define FOO() printf("Hello World\n")
I want to change it to
#define FOO() printf("===> Hello World <===")
Now, I know that in my code, X
, I can do:
#ifdef FOO
#undef FOO
#define FOO() printf("===> Hello World <===")
#endif
which will work fine for X
, but doesn't help for any code in Y
and compiling. I could modify Y
or L
's code locally, but then I can't check in my modifications as it might break other project's expectations of what FOO
does.
Is it possible to modify the macro via the command line, sort of in the same vain as -D
? If so, then I could change the behavior in my Makefiles and anyone that checks out X
will have access to the improved FOO
. The problem with using -D
is that it will define the macro function before X
and Y
#include
L's macro, which will then cause a macro redefinition warning (and subsequently not the macro I want).
Upvotes: 4
Views: 2639
Reputation: 218323
Assuming that L uses header guards, you may do something like
in l_improved.h
#ifndef L_IMPROVED_H
#define L_IMPROVED_H
#include "L.h"
#ifdef FOO
#undef FOO
#define FOO() printf("===> Hello World <===");
#endif
#endif
and then add:
-include l_improved.h
in your gcc compiler flags.
From gcc Preprocessor-Options:
-include file
: Process file as if #include "file" appeared as the first line of the primary source file.
Flag may be different if you use an other compiler.
Upvotes: 3
Reputation: 213200
I think the best overall fix is to modify the definition inside L
, like this:
#ifndef FOO
#define FOO() printf("Hello World\n")
#endif
You can check in this change without breaking anybody else's code, and it's then easy to override the definition of FOO
from the command line or from within a makefile.
Upvotes: 1
Reputation: 39400
I don't see a way to do that other than manipulating Y
files before building them (like appending the code you wrote).
This is really pretty straightforward and could be done with a make
command turning your files into say Y_modified.cpp
, and then making all of the build use the modified version. That way you could treat them as half-built files, and gain all benefits of incremental builds.
A single Y.cpp
including L.h
will turn into one translation unit that you can't really inject anything into after the preprocessor has ran.
Upvotes: 0