One of the header files contains the definition of a macro.

#define ERROR( ... ) log_message( NULL, EMIT_ERR, MODULE_NAME, __VA_ARGS__ ) 

Turning on windows.h I redefined this definition as follows:

 #define ERROR 0 

Accordingly, when compiling I get errors like:

 called object is not a function or function pointer ERROR( error ); ^ 

I understand that I can solve this by renaming the macro and replacing its name with 1 in all files by pressing a button, but I’m wondering if there is a more concise solution.

I have already tried to change the order of inclusion (put windows.h before the header file, in which there is a definition). I tried to insert the following construction before the definition:

 #ifdef ERROR #undef ERROR #endif 

But the error is still present.

  • Do you need it to work (log_message ..)? Then rename, and do not use more such simple names. PASAf_ERROR will do) - vp_arth
  • define error and define error(...) not the same ... - DNS
  • What exactly (small code example just give) you want to achieve? Rename macro to .c before broadcast? - avp
  • @avp, I want to preserve the functionality of both within the areas where they are used and avoid compilation errors. - PASAf
  • Catch the macros M and M(x) in response - avp

2 answers 2

If your code needs your definition of ERROR , then simply ensure that the order of including header files, with which your macro definition is always included in translation units after windows.h . In theory, it should be so by itself, if you practice to include system header files in the first place, and your own - only after them.

Before your definition of macro, of course, do not forget to do

 #ifdef ERROR #undef ERROR #endif 

In this case, you, of course, lose access to the windshield definition of ERROR , as well as to the RGN_ERROR defined through it (there are no more dependencies, like).

  • I did, but for some reason the error is still present. - PASAf

Not sure if this is the right approach.
(perhaps the best solution would be to simply edit the files by correctly changing the names of the macros),
but in principle, a similar thing can be represented like this:

 avp@avp-ubu1:hashcode$ cat tmac1.h #undef M #define M(x) puts("tmac1 M: "#x) avp@avp-ubu1:hashcode$ cat tmac2.h #undef M #define M puts("tmac2 M") avp@avp-ubu1:hashcode$ cat tmac.c #include <stdio.h> #include "tmac2.h" int main () { M; #include "tmac1.h" M(xoxo xxa); M(123); #include "tmac2.h" M; #include "tmac1.h" M(End); } avp@avp-ubu1:hashcode$ gcc tmac.c && ./a.out tmac2 M tmac1 M: xoxo xxa tmac1 M: 123 tmac2 M tmac1 M: End avp@avp-ubu1:hashcode$ 

Those. macros are scattered in different .h files, each of which is loaded in the right place in the code.

Update 1 (here, it occurred to me that the preprocessor can be used 2 times)

Somewhat complicating the compilation, of course, you can embellish the code, abandoning #include at the switch points between macros (replacing the #include directive with the switching macro).

For example, like this:

 #define USE1 \ #undef M \ #define M(x) puts("tmac1 M: "#x) #define USE2 \ #undef M \ #define M puts("tmac2 M") int main () { USE2 M; USE1 M(xoxo xxa); M(123); USE2 M; USE1 M(End); } 

And then:

 avp@avp-ubu1:hashcode$ gcc -E tmac.c | sed 's/#define/\n#define/' | gcc -o tmac1 -xc - avp@avp-ubu1:hashcode$ ./tmac1 tmac2 M tmac1 M: xoxo xxa tmac1 M: 123 tmac2 M tmac1 M: End avp@avp-ubu1:hashcode$ 

It works, as in the first version, additional .h files are not needed.

  • Well, this solution ... but it does not save me from having to insert in all files that use the second version of the USE2 macro (which is equivalent to including the definition, and requires exactly the same number of keystrokes) - PASAf
  • What did you want? Try to explain more clearly (and with examples of your code) - avp
  • Otherwise, it is not clear why the trivial #undef ERROR and then #define ERROR log_message(...) in your file included last does not work. - avp
  • If I could understand, I wouldn't go here. - PASAf