what is it for?

P

poison.summer

Hello, I know what ifdef is used for, but I am not sure what these
lines are for for compilation.

#ifdef __cplusplus
extern "C" {
#endif

Thanks a lot!
 
W

Walter Roberson

Hello, I know what ifdef is used for, but I am not sure what these
lines are for for compilation.
#ifdef __cplusplus
extern "C" {
#endif

Those lines are for use with C++. Parts of C++ are close enough to
parts of C to make it practical to use the C code within C++, except
that C++ has to be told to that the C code will be using C calling
conventions instead of C++ calling conventions. The #if you show
will have no effect on the code when processed by a C compiler, but
when processed by a C++ compiler then __cplusplus will be true,
causing extern "C" { } to be placed around the entire block of C
code -- which is the necessary signal to C++ about the calling conventions.
 
K

Kenneth Brody

Hello, I know what ifdef is used for, but I am not sure what these
lines are for for compilation.

#ifdef __cplusplus
extern "C" {
#endif

Note that there will be a corresponding

#ifdef __cplusplus
}
#endif

somewhere further down the file.

This tells a C++ compiler that the following is C code, rather than C++.

If you need more information on how this works, you'll need to ask over
in comp.lang.c++ rather than here.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 
K

Keith Thompson

Kenneth Brody said:
Note that there will be a corresponding

#ifdef __cplusplus
}
#endif

somewhere further down the file.

This tells a C++ compiler that the following is C code, rather than C++.

I don't think that's *quite* accurate. As I understand it, the code
is still C++ (it's being processed by a C++ compiler, after all); for
example, it can't use "class" as an ordinary identifier.
If you need more information on how this works, you'll need to ask over
in comp.lang.c++ rather than here.

Indeed -- but first check the comp.lang.c++ FAQ and/or any decent C++
textbook.
 
O

Old Wolf

Kenneth said:
This tells a C++ compiler that the following is C code, rather than C++.

Actually it doesn't. If you are using a C compiler, the code will
be treated as C, and if you are using a C++ compiler, the code
will be treated as C++.

This directive tells a C++ compiler to generate code that's
compatible with its C ABI, so that C and C++ object files can
subsequently be linked together. Examples of this include
disabling name mangling, and maybe different calling
conventions and structure padding.

Finally, nothing in the C standard prevents a C compiler from
defining __cplusplus, although only the DS9000 would do so.

I've attempted to set follow-ups to comp.lang.c++, because this
stuff is all part of C++ rather than C. I haven't done this before
so I hope it turns out well...
 
J

Jordan Abel

Finally, nothing in the C standard prevents a C compiler from
defining __cplusplus, although only the DS9000 would do so.

6.10.8 p5
|The implementation shall not predefine the macro __cplusplus, nor shall
|it define it in any standard header.
I've attempted to set follow-ups to comp.lang.c++, because this
stuff is all part of C++ rather than C. I haven't done this before
so I hope it turns out well...

It did, but I chose to ignore it because I'm replying to an aspect of
your post that only applies to C.
 
E

Eric Sosman

Old Wolf wrote On 12/05/05 17:12,:
[...]
Finally, nothing in the C standard prevents a C compiler from
defining __cplusplus, although only the DS9000 would do so.

Section 6.10.8, paragraph 5:

The implementation shall not predefine the
macro __cplusplus, nor shall it define it in
any standard header.
I've attempted to set follow-ups to comp.lang.c++, [...]

You did it correctly, but I added c.l.c. back
because the misteak should not stand unchallenged.
 
K

Keith Thompson

Old Wolf said:
Finally, nothing in the C standard prevents a C compiler from
defining __cplusplus, although only the DS9000 would do so.

Unless you count C99 6.10.7p5:

The implementation shall not predefine the macro __cplusplus, nor
shall it define it in any standard header.

(An aside: when I did a copy-and-paste from n1124.pdf, the macro name
showed up as "_ _cplusplus", which might make it difficult to find; I
searched for just "cplusplus".)
I've attempted to set follow-ups to comp.lang.c++, because this
stuff is all part of C++ rather than C. I haven't done this before
so I hope it turns out well...

It did, but I overrode the "Followup-To:" header for this article.
 
M

Mark McIntyre

Unless you count C99 6.10.7p5:

The implementation shall not predefine the macro __cplusplus, nor
shall it define it in any standard header.

(An aside: when I did a copy-and-paste from n1124.pdf, the macro name
showed up as "_ _cplusplus", which might make it difficult to find; I
searched for just "cplusplus".)


It did, but I overrode the "Followup-To:" header for this article.

Is there an echo in here?
:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,172
Messages
2,570,933
Members
47,472
Latest member
blackwatermelon

Latest Threads

Top