Before the GCC or Clang compiler compiles C source code into machine assembly, the C Preprocessor (cpp) scans the source file for directives starting with #. Preprocessor conditional directives (#ifdef, #else, #endif) allow developers to conditionally include or exclude blocks of code at compile time.
Cross-Platform Code Selection Example
#include <stdio.h>
// Conditionally compile code based on target OS macros
#if defined(_WIN32)
#define PLATFORM_NAME "Windows OS"
#include <windows.h>
#elif defined(__linux__)
#define PLATFORM_NAME "Linux OS"
#include <unistd.h>
#else
#define PLATFORM_NAME "Unknown OS Target"
#endif
int main(void) {
printf("Compiling application binary for: %s\n", PLATFORM_NAME);
#ifdef DEBUG_BUILD
printf("DEBUG MODE ACTIVE: Printing extra memory diagnostic logs.\n");
#else
printf("RELEASE MODE: Logging suppressed.\n");
#endif
return 0;
}Passing Preprocessor Macros via GCC (-D Flag)
# Define DEBUG_BUILD macro dynamically at compile time
gcc -DDEBUG_BUILD platform_os.c -o app_debug
# Run compiled debug binary
./app_debug
Comments and corrections