Pros use Conditional Compilation
Let's explore conditional compilation in C with detailed examples, including the use of #ifdef
, #ifndef
, #else
, #endif
, and #undef
. We'll also incorporate bash scripting and environment variables to demonstrate dynamic control over compilation.
Example: Simple Conditional Compilation
// main.c
#include <stdio.h>
// Uncomment the following line to enable debugging
#define DEBUG
int main() {
#ifdef DEBUG
printf("Debugging is enabled.\n");
#else
printf("Debugging is disabled.\n");
#endif
return 0;
}
In this example, the #ifdef DEBUG
checks if the symbol DEBUG
is defined. If defined, the code within the #ifdef
block is included during compilation; otherwise, the code within the #else
block is included. If the DEBUG
line is uncommented, the output will be:
Debugging is enabled.
Example: Using Bash and Environment Variables
// main.c
#include <stdio.h>
#ifdef ENABLE_FEATURE
#define FEATURE "Feature is enabled."
#else
#define FEATURE "Feature is disabled."
#endif
int main() {
printf("%s\n", FEATURE);
return 0;
}
Bash script (compile_and_
run.sh
):
#!/bin/bash
# Enable or disable the feature by setting the environment variable
export ENABLE_FEATURE=true
# Compile the C code
gcc main.c -o my_program
# Run the compiled program
./my_program
If you run the bash script with ENABLE_FEATURE=true
, the output will be:
Feature is enabled.
If you run the bash script with ENABLE_FEATURE=false
, the output will be:
Feature is disabled.
This demonstrates how you can control the compilation behavior using environment variables.
Example: Conditional Compilation with #else and #undef
// main.c
#include <stdio.h>
// Uncomment the following line to enable debugging
#define DEBUG
int main() {
#ifdef DEBUG
printf("Debugging is enabled.\n");
#else
printf("Debugging is disabled.\n");
#endif
#ifdef RELEASE
printf("Release mode is enabled.\n");
#else
printf("Release mode is disabled.\n");
#endif
// Undefine DEBUG for the rest of the program
#undef DEBUG
#ifdef DEBUG
printf("This line won't be printed because DEBUG is undefined.\n");
#endif
return 0;
}
In this example, DEBUG
is initially defined, but then it's undefined using #undef DEBUG
. After the #undef
directive, attempting to use DEBUG
will result in a compilation error.
Compilation and Execution
To compile and run these examples, you can use the following bash commands:
# Compile and run Example 1
gcc main.c -o example1
./example1
# Compile and run Example 2
chmod +x compile_and_run.sh
./compile_and_run.sh
# Compile and run Example 3
gcc main.c -o example3
./example3
These examples illustrate how conditional compilation can be utilized in different scenarios, allowing you to control the compilation process based on preprocessor directives and external factors such as environment variables.
Subscribe to my newsletter
Read articles from Jyotiprakash Mishra directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Jyotiprakash Mishra
Jyotiprakash Mishra
I am Jyotiprakash, a deeply driven computer systems engineer, software developer, teacher, and philosopher. With a decade of professional experience, I have contributed to various cutting-edge software products in network security, mobile apps, and healthcare software at renowned companies like Oracle, Yahoo, and Epic. My academic journey has taken me to prestigious institutions such as the University of Wisconsin-Madison and BITS Pilani in India, where I consistently ranked among the top of my class. At my core, I am a computer enthusiast with a profound interest in understanding the intricacies of computer programming. My skills are not limited to application programming in Java; I have also delved deeply into computer hardware, learning about various architectures, low-level assembly programming, Linux kernel implementation, and writing device drivers. The contributions of Linus Torvalds, Ken Thompson, and Dennis Ritchie—who revolutionized the computer industry—inspire me. I believe that real contributions to computer science are made by mastering all levels of abstraction and understanding systems inside out. In addition to my professional pursuits, I am passionate about teaching and sharing knowledge. I have spent two years as a teaching assistant at UW Madison, where I taught complex concepts in operating systems, computer graphics, and data structures to both graduate and undergraduate students. Currently, I am an assistant professor at KIIT, Bhubaneswar, where I continue to teach computer science to undergraduate and graduate students. I am also working on writing a few free books on systems programming, as I believe in freely sharing knowledge to empower others.