In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, the editor will share with you the relevant knowledge points about how to achieve conditional compilation in C language. the content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article, let's take a look at it.
In general, we want the program to execute selectively, often using branch statements, such as if-else or switch-case. Sometimes, however, a branch may not execute at all while the program is running.
For example, we need to write a cross-platform project that requires the project to run under both Windows and Linux. At this point, if we use if-else, it looks like this:
Windows has a proprietary macro _ WIN32,Linux has a proprietary macro _ _ 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")
There are two problems with this code: 1. There is no definition of _ _ linux__, under Windows, and there is no error when compiling. Similarly, _ WIN32 is not defined in Linux. 2. Assuming that this program can be run, it is impossible to run the code of the other two branches in the Windows environment, just as it is in Linux.
We can use conditional compilation to deal with this situation. Conditional compilation, as the name implies, is to selectively compile according to certain conditions. The effect we want to achieve is that the statements of the other two branches in the Windows environment will not be compiled at all, so that there will be no machine code for the corresponding statements in the generated executable file, which not only improves the compilation efficiency, but also reduces the size of the executable file.
Conditional compilation can usually be implemented in three ways:
1. # if--#elif--#else--#endif statement implementation
The code implemented in 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
When using this method, it should be noted that the macro is defined as real # if before it is executed, that is to say:
If there is a macro definition # define _ WIN32 0, # if will not be executed at this time. Need to be defined as # define _ WIN32 1 to execute
2. Realize it through # ifdef--#else--#endif statement
The code implemented in this way is
# ifdef (_ WIN32) printf ("Code executed under Windows\ n"); # else printf ("Code executed under Linux\ n"); # endif
In this method, you only need to define _ WIN32, which is not necessary to be true, that is to say,
The # ifdef statement above # define _ WIN32 0 can also be executed if there is a macro definition, and even the # ifdef above # define _ WIN32 can be 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 should be noted that in this case, the value of _ _ linux__ must be true in order for the # elif statement to execute! (there is also no definition of _ WIN32)
3. Use the # ifndef statement, which is similar to the second case, where ifndef is executed if no macro is defined.
In the gcc compilation tool
We can use the-D option to dynamically define the macros needed 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 Windows environment, _ WIN32 has been defined) the-D option in gcc defines the macro as 1 by default, if you want to define it for other values, use the equals sign such as:-D _ WIN32=0
Most of the time, especially in actual projects, we use cmake tools to build our own programs.
In cmake
We can write ADD_DEIFNITIONS (- D _ WIN32) in CMakeLists.txt to add macros that are used when the program is running. But in this way, once we need to modify the macros we use, we have to modify the CMakeLists.txt file, which will be very troublesome.
At this point, we can do this:
Write in CMakeLists.txt
IF (ENVIRO) ADD_DEFINITIONS (- D _ WIN32) ENDIF (ENVIRO)
In this way, we can add the-D option when using the cmake command, and define the ENVIRO command as follows
Cmake-D ENVIRO=1, or cmake-D ENVIRO=ON
To cancel this definition, you can use: cmake-D ENVIRO=OFF or cmake-D ENVIRO=0 or cmake-U ENVIRO
These are all the contents of the article "how to achieve conditional compilation in C language". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to 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.