In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
In this article, the editor introduces in detail "how to use conditional compilation of C language". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to use conditional compilation of C language" can help you solve your puzzles. the following is followed by the editor's ideas slowly in depth, together to learn new knowledge.
I. basic concepts
The behavior of conditional compilation is similar to if...else... in C
Compilation is a precompiled instruction command that controls whether a piece of code is compiled.
Let's take a look at a simple piece of conditional compiled code:
# include # define C 1 int main () {const char* s; # if (C = = 1) s = "This is first printf...\ n"; # else s = "This is second printf...\ n"; # endif printf ("% s", s); return 0;}
The following is the output:
You can enter the gcc-E Test.c-o file.i command to see what happened in the precompilation phase. Here is some of the output:
# 2 "Test.c" 2 int main () {const char* s; s = "This is first printf...\ n"; printf ("% s", s); return 0;}
You can see that the macro definition and conditional compilation are gone and replaced by the corresponding content.
II. The essence of conditional compilation
The precompiler selectively deletes the code according to the conditional compilation instructions
The compiler is not aware of the existence of a code branch
If...else... Statement makes branch judgment at run time
Branch judgment of conditional compilation instructions during precompilation period
Macros can be defined from the command line
Gcc-Dmacro=value file.c
Gcc-Dmacro file.c
Let's take a look at a code that defines a macro through the command line:
# include int main () {const char* s; # ifdef C s = "This is first printf...\ n"; # else s = "This is second printf...\ n"; # endif printf ("% s", s); return 0;}
The terminal inputs gcc-DC Test.c, and the output is as follows:
III. The essence of # include
The essence of # include is to embed the contents of an existing file into the current file
The indirect inclusion of # include also results in operations that embed the contents of the file.
This raises the question of whether indirectly including the same header file will result in a compilation error.
Let's delve deeper through a piece of code:
Global.h:
/ / global.hint global = 10
Test.h:
/ / test.h # include "global.h" const char* NAME = "test.h"; char* hello_world () {return "hello world!\ n";}
Test.c:
# include # include "test.h" # include "global.h" int main () {const char* s = hello_world (); int g = global; printf ("% s\ n", NAME); printf ("% d\ n", g); return 0;}
After compilation, the compiler reports an error, and global redefines:
Why is global redefined? To start step-by-step compilation, enter gcc-E test.c-o test.i, and the output is as follows:
# 2 "test.c" test.h 1 "test.h" 1 # 1 "global.h" 1 int global = 10 hello world # 4 "test.h" 2 const char* NAME = "test.h"; char* hello_world () {return "hello world!\ n";} # 3 "test.c" 1 "global.h" 1 int global = 10 X # 4 "test.c" 2 int main () {const char* s = hello_world (); int g = global Printf ("% s\ n", NAME); printf ("% d\ n", g); return 0;}
It is obvious that the program first copies the contents of test.h into test.c, and since there is an include "global.h" in test.h, it copies int global = 10;, and then copies it.
Const char* NAME = "test.h"
Char* hello_world ()
{undefined
Return "hello world!\ n"
}
Then a # include "global.h" is defined in the test.c, and the int global = 10; is copied, resulting in a duplicate definition.
Conditional compilation can solve the compilation errors caused by repeated header files.
# ifndef _ HEADER_FILE_H_#define _ HEADER_FILE_H_//source code#endif
If no header_file.h is defined, the code inside is defined and executed; otherwise, if it is defined, the code inside is not executed.
So this can be changed in the above code:
Global.h:
/ / global.h#ifndef _ GLOBAL_H_#define _ GLOBAL_H_int global = 10; # endif
Test.h:
/ / test.h#ifndef _ TEST_H_#define _ TEST_H_#include "global.h" const char* NAME = "test.h"; char* hello_world () {return "hello world!\ n";} # endif
So the compilation can pass.
IV. The significance of conditional compilation
Conditional compilation allows us to compile different code segments according to different conditions, thus producing different object code.
# if...#else...#endif is processed by the precompiler, while if...else... Statements are processed by the compiler and are bound to be compiled into the object code
In the actual project, conditional compilation is mainly used in the following two situations:
Different product lines share the same code
Distinguish between debug and release versions of compiled products
Let's take a look at a product line differentiation and debugging code:
Product.h:
# define DEBUG 1#define HIGH 1
Test.c:
# include # include "product.h" # if DEBUG # define LOG (s) printf ("[% SV% d]% s\ n", _ _ FILE__, _ _ LINE__, s) # else # define LOG (s) NULL#endif#if HIGHvoid f () {printf ("This is the high level product!\ n");} # elsevoid f () {} # endifint main () {LOG ("Enter main ()..."); f () Printf ("1. Query Information.\ n"); printf ("2. Record Information.\ n"); printf ("3. Delete Information.\ n"); # if HIGH printf ("4. High Level Query.\ n"); printf ("5. Mannul Service.\ n"); printf ("6. Exit.\ n"); # else printf ("4. Exit.\ n"); # endif LOG ("Exit main ()...") Return 0;}
Macro DEBUG refers to whether the product is debugged or released. The debug version is 1 and the release version is 0. Macro HIGH refers to whether the product is a high-end product or a low-end product, high-end product is 1 and low-end product is 0.
If we want to test the debug version of the high-end products, we can make DEBUG 1 and high 0:
By the same token, we want to test the low-end products of the release version so that DEBUG is 0 and high is 0:
After reading this, the article "how to use conditional compilation of C language" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.