C-language Conditional compilation #if, #elif, #else, #endif, #ifdef, #ifndef_Embeder__C语言

Source: Internet
Author: User
Some programs are debugging, compatibility, in situations like platform porting, you might want to generate a different software by simply setting some parameters, which, of course, can be done with variable settings, write all the possible code, and configure it when initialized, but in different situations you might use only a subset of the code, There is no need to write all the code, you can use conditional compilation, compile the instructions to set the compilation conditions, different needs, compile different code. (i) Conditional compilation method

Conditional compilation is accomplished through precompiled directives, with the following main methods:

1, #if, #elif, #else, #endif

#if condition 1 Code Snippet 1 #elif condition 2 Code Snippet 2 ... #elif condition n code snippet N #else code snippet n+1 #endif

That is, you can set different conditions, compile different code at compile time, the expression in the precompiled instruction and C language itself basic one to such as logical operation, arithmetic operation, bit operation, etc. can be used in the precompiled instruction. Conditional compilation is possible because precompiled directives are processed before compiling, by precompiling for macro substitution, conditional selection code snippets, and then generating the final code to be compiled, and finally compiling.
#if的一般含义是, if the constant expression after #if is true, compile the code that it controls, such as when condition 1 is set up in code Segment 1, Condition 1 is not tenable, and then the condition 2 is set up, if the condition 2 is established then the Code section 2 is compiled, otherwise the other conditions are judged by analogy, If the conditions are 1-n, the final code snippet will be compiled n+1.
2, #ifdef, #else, #endif或 #ifndef, #else, #endif

Another method of conditional compilation is to use the #ifdef and #ifndef commands, which represent "if defined" and "if Not defined" respectively. is defined as whether there is a macro defined by a macro #define instruction when compiling this piece of code. #ifndef指令指找不到通过 a macro defined by #define, which can be defined on the side of this instruction in the current file, or in other files, but included in the file before this instruction.

#ifdef的一般形式是:

#ifdef macro_name Code Snippet 1 #else code snippet 2 #endif or #ifdef的一般形式是: #ifndef macro_name Code Snippet 2 #else code segment 1 #endif
The effect of the two pieces of code is exactly the same.

3, through the macro function defined (macro_name)

parameter is the name of the macro (without "") and if the macro_name is defined it returns TRUE, otherwise it returns false, and with this function you can write more complex conditional compilation directives such as

#if defined (Macro1) | | (!defined (MACRO2) && defined (MACRO3)) ... #else ... #endif (ii) conditional compilation techniques and examples

(1) Comparison of #ifdef和 #defined ()

First of all, compare the two methods, the first method can only judge a macro, if the conditions are more complex to achieve more annoying lock, the latter is more convenient. If you have two macro macro_1,macro_2 only two macros are defined before you compile code snippet A, implement the following:

#ifdef macro_1 #ifdef macro_2 code snippet A #endif #endif or #if defined (macro_1) && defined (macro_2) #endif

Similarly, to implement more complex conditions with #ifdef is more troublesome, so it is recommended to use the latter, because even if the current code with a simple conditional compilation, later in the maintenance, upgrade may increase, with the latter can be more maintainable. The old compiler may not have implemented the #defined () directive, and C99 has been added to the standard. To be compatible with the old compiler, you need to #ifdef instructions.

(2) Comparison of #if与 #ifdef或 #if defined ()

For example, you write a PRINTF function, want to implement conditional compilation through a macro my_printf_en, with #if can be implemented as follows


#define MY_PRINTF_EN 1
#if mys_printf_en = = 1 int PRINTF (char* fmt, char* args, ...) {     ... } #endif

If the macro my_printf_en is defined as 1, the code is compiled, and if the macro definition is not 1 or the macro is not defined, the code is not compiled. It can also be implemented through #ifdef or #defined (), such as

#define MY_PRINTF_EN 1 #if defined (my_printf_en) int PRINTF (char* fmt, char* args, ...) {     ... } #endif

In this case, the two methods are similar to each other, but imagine if you write two printf functions in order to save code, use different printf functions in different situations, one is a lite version and one is a full-featured Standard Edition, such as:

#define My_printf_simple #ifdef my_printf_simple void PRINTF (*STR)//to the terminal to simply output a string {...} #endif #ifdef My_printf_stan dard int printf (char* fmt, char* args, ...) {... }     #endif can also be implemented with #if defined () #define MY_PRINTF_SIMPLE #if defined (my_printf_simple) void PRINTF (*STR)//To simply output a string to the terminal { ... } #elif defined (my_printf_standard) int PRINTF (char* fmt, char* args, ...) {     ... } #endif

Both methods can be implemented, but the latter is more convenient. But imagine if you have three versions, the former is even more troublesome, but the method is similar, the latter is more convenient, but still need three macros to control, you have to live three macros, improve the use of #if can use a macro directly control n cases such as:

#define My_printf_version 1 #if my_printf_version = = 1 void PRINTF (*STR)//to the terminal simply output a string {...} #elif My_prin Tf_version = = 2 int printf (char* fmt, char* args, ...) {     ... } #elif my_printf_version = = 3 int PRINTF (unsigned char com_number, char* str) {...} #else default version #endif

In this way, you can only modify the number to complete the version of the choice

It seems to be better to use #if, imagine the following: You write a configuration file called Config.h used to configure some macros, through these macros to control the code, such as your CONFIG.H macro

#define MY_PRINTF_EN 1

To control whether you need to compile your printf function, and the following instructions in your source code file PRINTF.C

#i nclude "Config.h" #if my_printf_en = = 1 int PRINTF (char* fmt, char* args, ...) {     ... } #endif

But there is also a problem, that is, if you forget to add a macro my_printf_en to the config.h, the PRINTF function you wrote will not be compiled, and some compilers will give a warning: my_printf_en undefined. If you have two versions of the want to have a default version, you can implement this in PRINTF.C

#incldue "Config.h" #if!defined (my_printf_version) #define My_printf_version 1 #endif #if my_printf_version = 1 V OID printf (*STR)//to the terminal simply output a string {...} #elif my_printf_version = = 2 int printf (char* fmt, char* args, ...) {     ... } #elif my_printf_version = = 3 int PRINTF (unsigned char com_number, char* str) {...} #endif

In this case, you have to use the #ifdef or #if defined (), you can not move the main body of any code, only need to modify the PRINTF.C file My_rpintf_version macro number can be changed, if you use the previous method to drag the code, It is possible to cause an error in the drag.

Imagine, if the software upgrades, or have a big change, there are three versions, now only two versions, such as

#if my_printf_version = = 2 int PRINTF (char* fmt, char* args, ...) {     ... } #elif my_printf_version = = 3 int PRINTF (unsigned char com_number, char* str) {...} #endif

Because these core codes don't want the people who use the code to care, they just need to modify the Config.h file, and that's the compatibility in the PRINTF.C. If someone had previously configured macro my_printf_version to be 1 in config.h, there

#define My_printf_version 1

And now there is no 1 version, to be compatible how to do. Of course, it can be achieved with more complex conditions such as:

#if my_printf_version = 2 | | My_printf_version = = 1 int PRINTF (char* fmt, char* args, ...) {     ... } #elif my_printf_version = = 3 int PRINTF (unsigned char com_number, char* str) {...} #endif

There is another way, though, to use the #undef command

#if my_printf_version = 1 #undef my_printf_version #define My_printf_version 2 #endif #if my_printf_version = = 2 in T printf (char* fmt, char* args, ...) {     ... } #elif my_printf_version = = 3 int PRINTF (unsigned char com_number, char* str) {...} #endif

The advantage of using #if is that if you make the macro name wrong and define my_printf_en as My_print_en, then the code you control with #ifdef my_printf_en or #if defined (my_printf_en) cannot be compiled, It is not easy to check, with #if my_printf_en ==1 control is very good to check, because you define my_printf_en as My_print_en, then the my_printf_en is not actually defined, then the compiler will give a warning #if my_printf_ The my_printf_en in EN = = 1 are not defined, but they are more rapid.



Zz:http://blog.sina.com.cn/s/blog_4b4b54da0100r2l6.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.