Override Macro in Existing C Library

Let's say that I have the 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 Lthere is a macro that is widely used in code XandY

#define FOO() printf("Hello World\n")

I want to change it to

#define FOO() printf("===> Hello World <===")

Now I know what in my code XI can do:

#ifdef FOO
    #undef FOO
    #define FOO() printf("===> Hello World <===")
#endif

which works great for Xbut doesn't help for any code in Yand compilation. I could change the code Yeither Llocally, but then I can’t check my modifications, as this can violate other expectations of the project, which does FOO.

, , -D? , Makefile, , X, FOO. -D , X Y #include L, ( , ).

+4
3

, L , -

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

:

-include l_improved.h

gcc.

gcc Preprocessor-Options:

-include file: , #include "file" .

, .

+3

, L, :

#ifndef FOO
  #define FOO() printf("Hello World\n")
#endif

, , FOO makefile.

+1

, Y, (, , ).

It is really quite simple and can be done with a command makethat turns your files into say Y_modified.cpp, and then use the modified version when creating the entire assembly. Thus, you can consider them as half-line files and get all the benefits of incremental builds.

The only one Y.cppthat includes L.hwill turn into one translation unit into which you cannot insert anything after the preprocessor starts.

0
source

Source: https://habr.com/ru/post/1611813/


All Articles