简体   繁体   中英

Does the Visual C++ compiler optimize out undefined macro blocks?

Take this code for example:

#ifdef THIS_IS_NOT_DEFINED
//lots of code here...
#endif

Say that the "lots of code" could potentially add a megabyte to the resulting executable. If THIS_IS_NOT_DEFINED is indeed not defined, then will the compiler still write all the contained code to the .exe, or not? Do most compilers follow a similar procedure for this?

The C preprocessor processes the #ifdef. If it is not defined, then the compiler itself doesn't even see the code, so there's no way for it to get into the .exe.

C++ compilation works in many phases. First, the code is preprocessed by expanding out #include s, #define s, etc. This works at a textual level and is equivalent to actually modifying the source code.

In this case, the compiler will optimize out the code from the preprocessor #ifdef block because as the preprocessor runs it will splice that code out of the source file. In other words, the part of the compiler that actually does compilation and code generation will never see that part of the code at all. It's as if it doesn't exist.

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM