I've see in some code:
>
#ifdef __cplusplus
extern "C"
{
#endif
>
what does it mean?
It means that someone hasn't decided which language they're writing in.
Best avoided.
--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
In article <47fcee3f$0$416 59$4fafbaef@rea der4.news.tin.i t>,
mattia <gervaz@gmail.c omwrote:
>I've see in some code:
>#ifdef __cplusplus
extern "C"
{
>#endif
>
>what does it mean?
This has to do with C++. A C compiler will not defne __cplusplus
and so a C compiler will compile those lines to nothingness.
This form detects that whether the code is being compiled with a C++
compiler, and if it is, wraps extern "C" { } around the block of code.
The meaning of extern "C" { } is specific to C++, and in C++ means
that the enclosed block of code is to have its functions compiled
with C calling sequences rather than C++ calling sequences. The
single most important difference between C calling sequences and C++
calling sequences is that C++ calling sequences (usually) have
a hidden parameter named "this" that refers to the object being
operated upon; using this syntax in C++ tells C++ to not put in
that hidden parameter, thus leaving the functions defined suitable for
calling from C (or, alternately, -declaring- functions as being
external functions that need C-style parameters to call.)
--
"When we all think alike no one is thinking very much."
-- Walter Lippmann
I've see in some code:
>
#ifdef __cplusplus
extern "C"
{
#endif
>
what does it mean?
C++ supports certain powerful but complex features that mean that
different functions and variables can have different names:
specifically, in C++ function names can be overloaded (two functions can
have the same name, but different argument signatures), and also C++
supports "namespaces ", which granulate visibility in C's single global
external namespace.
To get around these problems, C++ compilers commonly "mangle" function
names: that is, the function name that appears in the global namespace
will be a string obtained in some way or other from the actual name of
the function as it appears in the code, the type and number of its
arguments, and any namespace qualifications needed to specify it.
This means that if a C++ program tries to use functions from a C
library, then there will be problems: when the C++ linker sees a
function called foobar, it will actually look in the symbol table for
something like foorbar__Fii.
To get around this, there's a way for the programmer to tell his
C++ linker that some of the functions have "C linkage", and so it
shouldn't try to mangle their names. This is achived by surrounding the
code with
extern "C" {
....
}
Sometimes, people write functions in C and then want to create a single
header that will let people use their functions either in C or C++.
To accomplish this, they make use of the fact that the macro __cplusplus
will be defined by the compiler if and only if it's compiling the
program as C++, and include the extern "C" lines conditional on the
presence of this macro. Therefore, if the file is being compiled as C
then this little bit of C++ will vanish during preprocessing, and won't
choke the C compiler.
I've see in some code:
>
#ifdef __cplusplus
extern "C"
{
#endif
>
what does it mean?
>
Thanks
Others have stated what it means; I'd just add that I've usually seen
this idiom used in system header files (such as stdio.h) so that the
same header can be used for both C and C++ compilers. C compilers (DS9K
excluded) don't define __cplusplus and don't see the extern "C" bit,
while C++ compilers do define it and so know that all the stdio.h
functions declared are C-style functions and linked in a C-style way.
I wouldn't recommend this practice in your own code.
extern "C" has no defined meaning in C. You'd probably get better
answers in comp.lang.c++.
>I've see in some code:
>>
>#ifdef __cplusplus
> extern "C"
> {
>#endif
>>
>what does it mean?
>
C++ supports certain powerful but complex features that mean that
different functions and variables can have different names:
specifically, in C++ function names can be overloaded (two functions can
have the same name, but different argument signatures), and also C++
supports "namespaces ", which granulate visibility in C's single global
external namespace.
>
To get around these problems, C++ compilers commonly "mangle" function
names: that is, the function name that appears in the global namespace
will be a string obtained in some way or other from the actual name of
the function as it appears in the code, the type and number of its
arguments, and any namespace qualifications needed to specify it.
>
This means that if a C++ program tries to use functions from a C
library, then there will be problems: when the C++ linker sees a
function called foobar, it will actually look in the symbol table for
something like foorbar__Fii.
>
To get around this, there's a way for the programmer to tell his
C++ linker that some of the functions have "C linkage", and so it
shouldn't try to mangle their names. This is achived by surrounding the
code with
extern "C" {
...
}
>
Sometimes, people write functions in C and then want to create a single
header that will let people use their functions either in C or C++.
>
To accomplish this, they make use of the fact that the macro __cplusplus
will be defined by the compiler if and only if it's compiling the
program as C++, and include the extern "C" lines conditional on the
presence of this macro. Therefore, if the file is being compiled as C
then this little bit of C++ will vanish during preprocessing, and won't
choke the C compiler.
Very well explained. I'm amazed no one has scolded you and told the OP
that the "experts" are down the corridoor second on the left ....
>>#ifdef __cplusplus
>>extern "C"
>>{
>>#endif
>>>
>>what does it mean?
>>
>This has to do with C++. A C compiler will not defne __cplusplus
>and so a C compiler will compile those lines to nothingness.
>
My DS9K defines __cplusplus.
>
(By which I mean, there's nothing in the C Standard preventing a
conforming implementation defining __cplusplus, though it would be a
Dumb Thing for a C implementation to do, and I'd be happy relying on my
implementation not to define it.)
In section 6.10.8 (Predefined macro names) paragraph 5 read:
The implementation shall not predefine the macro __cplusplus, nor
shall it define it in any standard header.
>My DS9K defines __cplusplus.
>>
>(By which I mean, there's nothing in the C Standard preventing a
>conforming implementation defining __cplusplus, though it would be a
>Dumb Thing for a C implementation to do, and I'd be happy relying on my
>implementati on not to define it.)
>
In section 6.10.8 (Predefined macro names) paragraph 5 read:
>
The implementation shall not predefine the macro __cplusplus, nor
shall it define it in any standard header.
>
Well I never! I stand completely and utterly corrected. Is that in C89 too?
In article <slrnfvq0v8.479 .nospam@nospam. invalid>,
Antoninus Twink <nospam@nospam. invalidwrote:
>On 9 Apr 2008 at 17:48, Richard wrote:
>Very well explained. I'm amazed no one has scolded you and told the OP
>that the "experts" are down the corridoor second on the left ....
>
>Thanks.
>
>Luckily they've all kill-filed me, so I can safely post useful
>information without generating a dozen flames in response.
>
It sounds like you have actually solved the problem of CLC.
Congratulations .
Essentially, there will be two CLCs - the one populated and run by the
regs (which, as we know, is 65% endless topicality BS, 30% beginner
questions and answers[*], and 5% [boring] lawyering) - and the other
one (unseen, as you say, by the regs) populated by the rest of us.
[*] By the way, tell me again, how _do_ you prototype main()?
>This has to do with C++. A C compiler will not defne __cplusplus
>and so a C compiler will compile those lines to nothingness.
>
My DS9K defines __cplusplus.
In which case you should upgrade to the C99 compiler for the DS9K.
(By which I mean, there's nothing in the C Standard preventing a
conforming implementation defining __cplusplus, though it would be a
Dumb Thing for a C implementation to do, and I'd be happy relying on my
implementation not to define it.)
<snip>
C99 banned implementations from defining __cplusplus
--
Flash Gordon
>I've see in some code:
>>
>#ifdef __cplusplus
> extern "C"
> {
>#endif
>>
>what does it mean?
>>
>Thanks
>
Others have stated what it means; I'd just add that I've usually seen
this idiom used in system header files (such as stdio.h) so that the
same header can be used for both C and C++ compilers. C compilers (DS9K
excluded) don't define __cplusplus and don't see the extern "C" bit,
while C++ compilers do define it and so know that all the stdio.h
functions declared are C-style functions and linked in a C-style way.
>
I wouldn't recommend this practice in your own code.
>
extern "C" has no defined meaning in C. You'd probably get better
answers in comp.lang.c++.
>
Philip
The answer has been given exactly as is more than once. Traipsing over
there is slightly overkill IMO.
>>#ifdef __cplusplus
>>extern "C"
>>{
>>#endif
>>>
>>what does it mean?
>>
>This has to do with C++. A C compiler will not defne __cplusplus
>and so a C compiler will compile those lines to nothingness.
>
My DS9K defines __cplusplus.
>
(By which I mean, there's nothing in the C Standard preventing a
conforming implementation defining __cplusplus, though it would be a
Dumb Thing for a C implementation to do, and I'd be happy relying on my
implementation not to define it.)
Nonsense. That would make a mockery of the whole thing.
Comment