Search code examples
compilationmacrosc-preprocessor

Will compiler -D flag take priority over code #define of macro variable?


I have some testing code in Fortran which basically looks like

#define test1 0 
#define test2 0 
#define test3 0 

... 

#if test1
   call test1() 
#endif 

#if test2
   call test2() 
#endif 

#if test3 
   call test3() 
#endif 

... 

At compilation, i want to change those values using -Dtest1=1. I've seen some answers (like here) where they say you need to put

#ifndef test1
    #define test1 0 
#endif 

But i have a lot of those and i would prefer not to add 40 lines of definition code. Will compilation command -D flag take priority over my hard definition without the if clause?

EDIT : So i just tested it (which i should have done anyway before asking...) and compiler -D flag does not take priority over my own definition and just pulls out a warning . So any way for it to take priority and always use the compiler flags without passing through ifndef clauses?


Solution

  • -D effectively adds a #define at the beginning, before reading the input. So it would be just like adding an additional #define in your source code, which would naturally produce an error (or warning) about redefining the macro.

    If you want to avoid that error, use the #ifndef idiom, just like everyone else does.