简体   繁体   中英

What does #ifndef _Python_CALL mean in a C++ code?

I am debugging a C++ code which contains something related to Python. In a function:

void normalize(string &text) {

...

#ifdef _Python_CALL
    newContentStr = contentStr;
#endif
#ifndef _Python_CALL
   ...
   ...
#endif

return 0;

}

I am using GDB to keep track of the code logic, and I found that after it reaches the line:

newContentStr = contentStr;

It then directly jumps to the last line in the function:

return 0;

Why is the code between the following is skipped?

#ifndef _Python_CALL
       ...
       ...
    #endif

Also note that the first is "#ifdef" and the 2nd is "#ifndef". Does that make the skip?

#ifndef is the opposite of #ifdef .

In your case, #ifdef is true, so it jump to return 0 directly and omit #ifndef block.

see this official doc

Judging from the code fragment you've shown, _Python_CALL is a macro name, possibly defined somewhere via #define _Python_CALL (or maybe by some other means, such as using a command line argument to the C++ compiler during compilation).

Then, line #ifdef _Python_CALL means that everything that follows it until the line #endif will be compiled (and thus executed in the compiled program) if and only if the macro name _Python_CALL is defined (the #ifdef means "if defined"). Since you claim that the line newContentStr = contentStr; was executed, we can assume that the macro name _Python_CALL was indeed defined during compilation.

Now, the line #ifndef _Python_CALL means that everything that follows it until the line #endif will be compiled (and executed) if and only if the macro name _Python_CALL is NOT defined. (Note the n in #ifndef , it means "if not defined"). But, as we already know (from the conclusion we made in the previous paragraph), this is not the case, because _Python_CALL is indeed defined. Thus, this block will not be compiled/executed.

On Cppreference, you can read more about C++ preprocessor , especially about #define and #ifdef / #ifndef directives, to gain deeper understanding.

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