Normally, we want the program to execute selectively, using branching statements, such as If-else or switch-case. Sometimes, however, a branch may not execute at all during the course of a program's operation.
For example, we want to write a cross-platform project that requires the project to run under Windows and under Linux. This time, if we use If-else, as follows:
Windows has exclusive macros _WIN32 , Linux has exclusive macros__linux__
if (_WIN32) printf ("code executed under Windows \ n"); Else if (__linux__) printf ("code executed under Linux \ n"); Else printf (" unknown platform cannot run!\n");
This code has two questions: 1, under Windows does not define __LINUX__, compile time will be error, also in Linux does not define _WIN32. 2, assuming this program can run, then in the Windows environment, the other two branches of the code is impossible to run, the same is true under Linux.
We can use conditional compilation to handle this situation. Conditional compilation, as the name implies, is based on certain conditions for the selection of the compilation, we want to achieve the effect, is in the Windows environment two other branches of the statement will not compile, so that the resulting executable file, there will be no corresponding statement machine code, which improves the compilation efficiency, It also reduces the volume of executable files.
Conditional compilation can usually be implemented in three ways:
1, #if--#elif--#else--#endif语句实现
The code implemented by this method is:
#if (_WIN32) printf ("code executed under Windows \ n"); #elif (__linux__) printf ("code executed under Linux \ n"); #else printf (" unknown platform cannot run!\n"); #endif
In this way, it is important to note that the macro is defined as true if # before it executes, that is:
If there is a macro defining # define # _WIN32 0 This time # # will not execute. Need to be defined as # define _WIN32 1 to execute
2, through the #ifdef--#else--#endif语句实现
The code implemented in this way is
#ifdef (_WIN32) printf ("code executed under Windows \ n"); #else printf ("code executed under Linux \ n"); #endif
This method only needs to define the _WIN32, it is not necessary to be true, that is to say
If a macro defines a # define # _win32 0 above the #ifdef statement can also be executed, even the # define _WIN32 above the #ifdef can also run
Of course, you can also add the #elif statement in the first method
#ifdef (_WIN32) printf ("code executed under Windows \ n"); #elif (__linux__) printf ("code executed under Linux \ n"); #else printf (" unknown platform cannot run!\n"); #end
However, it is important to note that in this case, the value of the #elif statement execution __linux__ must be true! (_win32 not defined at the same time)
3, using the #ifndef statement, this situation is similar to the second, ifndef is if you do not define a macro, it is executed.
In the GCC compilation tool
We can use the-D option to dynamically define the macros required by the program
For example, we can compile GCC test.c-o test-d _win32 so that the program can run under Windows (of course, in the context of Windows, _WIN32 has been defined), the-D option in GCC will default to define the macro as 1, if you want to define Use the equals sign for other values such as:-D _win32=0
Many times, especially in actual projects, we use the CMake tool to build our own programs .
In the CMake
We can write Add_deifnitions (-D _win32) in CMakeLists.txt to add macros that the program uses when it runs. But then, once we need to modify the macro used, it will be troublesome to modify the CMakeLists.txt file.
At this point we can do this:
Write in CMakeLists.txt
IF (ENVIRO)
Add_definitions (-D _win32)
ENDIF (ENVIRO)
This allows us to add the-D option when using the CMake command, which defines the Enviro command as follows
Cmake-d enviro=1, or cmake-d enviro=on
If you want to cancel this definition you can use: cmake-d enviro=off or cmake-d enviro=0 or cmake-u ENVIRO
It's written here and I hope it helps you. The level is limited, there is the wrong place also please understanding, and invites correction.
Conditional compilation in the C language