Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trigger the __cplusplus (C++) #ifdef?

Tags:

c++

macros

#ifdef __cplusplus
// C++ code
#else
// C code
#endif

The structure is this. My question is, how to actually trigger the #ifdef on?

I mean, in program? What code I write can turn #ifdef on?

For example, in this case. is that

#define __cplusplus

will turn it on?

like image 526
David Degea Avatar asked Jul 21 '11 16:07

David Degea


People also ask

What is __ Cplusplus in C++?

__cplusplus. This macro is defined when the C++ compiler is in use. You can use __cplusplus to test whether a header is compiled by a C compiler or a C++ compiler. This macro is similar to __STDC_VERSION__ , in that it expands to a version number.

What does #ifdef mean in C++?

The meaning of #ifdef is that the code inside the block will be included in the compilation only if the mentioned preprocessor macro is defined. Similarily #if means that the block will be included only if the expression evaluates to true (when replacing undefined macros that appears in the expression with 0).

Where Cplusplus is defined?

C++ preprocessor macro __cplusplus The __cplusplus preprocessor macro is defined if the compilation unit is compiled with a C++ compiler. Its value corresponds to the C++ standard that the compiler uses to compile a compilation unit.

How do I know if G ++ supports C ++ 11?

To see if your compiler has C++11 support, run it with just the --version option to get a print out of the version number. Do this for whichever compiler(s) you wish to use with Rosetta. Acceptable versions: GCC/g++: Version 4.8 or later.


2 Answers

"#define __cplusplus"

will let it on?

Yes, it will "let it on".

__cplusplus should be automatically defined by C++ compiler. C++ uses different name mangling and the macro often used to make C headers compatible with C++:

#ifdef __cplusplus
extern "C" {
#endif

...

#ifdef __cplusplus
}
#endif
like image 94
kravemir Avatar answered Oct 27 '22 08:10

kravemir


Just compile it with a C++ compiler and __cplusplus is defined automatically in that case.

like image 20
R. Martinho Fernandes Avatar answered Oct 27 '22 09:10

R. Martinho Fernandes