Search code examples
cgccifndef

Multiple #ifndef statements - which one gets applied


Say I have five files: main.c, sample1.c, sample1.h, sample2.c and sample2.h, where in each of these files DEBUG_PRINTS is defined as follows:

#ifndef DEBUG_PRINTS
#define DEBUG_PRINTS 0
#endif

and the code is compiled using gcc:

gcc -Wall main.c sample1.c sample2.c -o main

Now I change the value of DEBUG_PRINTS in one file to 1. Without printing to the terminal how can you determine, which value will be applied?

Additionally, how can I define DEBUG_PRINTS locally per file - the same as using the static keyword for variables?


Solution

  • Each file is compiled separately. Macros from one file are not visible in any other file. Once the files are independently compiled, the resulting objects are linked together to create an executable.