Reputation: 1398
#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?
Upvotes: 17
Views: 26534
Reputation: 11026
"#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
Upvotes: 21
Reputation: 490338
A C++ compiler defines this automatically.
Since this starts with two consecutive underscores, it is reserved. You are not allowed to define it yourself (i.e., attempting to do so gives undefined behavior).
Upvotes: 10
Reputation: 146968
The C++ Standard enforces that __cplusplus
will always be defined in C++ programs. The C Standard obviously does not. This means that the user need go to no effort to enable this machinery.
Upvotes: 13
Reputation: 234584
Just compile it with a C++ compiler and __cplusplus
is defined automatically in that case.
Upvotes: 16