Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: #ifdef __cplusplus

Reply
Thread Tools

Re: #ifdef __cplusplus

 
 
Antoninus Twink
Guest
Posts: n/a
 
      04-09-2008
On 9 Apr 2008 at 16:26, mattia wrote:
> 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.

 
Reply With Quote
 
 
 
 
Richard
Guest
Posts: n/a
 
      04-09-2008
Antoninus Twink <> writes:

> On 9 Apr 2008 at 16:26, mattia wrote:
>> 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 ....

 
Reply With Quote
 
 
 
 
Antoninus Twink
Guest
Posts: n/a
 
      04-09-2008
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.

 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      04-09-2008
In article <>,
Antoninus Twink <> wrote:
>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()?

 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      04-09-2008
(Kenny McCormack) writes:

> In article <>,
> Antoninus Twink <> wrote:
>>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()?


aaargh! Did you cast malloc?

<Martin Ambuhl voice>Thats bullshit and I wont read any more. We don't
deal with morons here</snotty ego maniac voice>
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      04-09-2008
Richard <devr_@gmail.com> writes:
> Antoninus Twink <> writes:
>> On 9 Apr 2008 at 16:26, mattia wrote:
>>> I've see in some code:
>>>
>>> #ifdef __cplusplus
>>> extern "C"
>>> {
>>> #endif
>>>
>>> what does it mean?

>>

[off-topic explanation deleted]
>
> 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 ....


Would it do any good? AT is apparently determined to ignore the fact
that this newsgroup is for discussion of the C programming language.
Of course most of the regulars killfiled him long ago. They might
want to consider unkillfiling him, at least for a while, to see what
he's up to now.

Some of what he's written recently might be valuable *if* it were
posted in an appropriate forum.

Meanwhile, you and your friends continue to make no actual
contribution, preferring to spend your time whining about other
posters.

--
Keith Thompson (The_Other_Keith) <kst->
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with __cplusplus Arturo C Programming 12 05-09-2008 11:02 AM
Re: #ifdef __cplusplus Flash Gordon C Programming 10 04-11-2008 07:06 AM
Re: #ifdef __cplusplus Walter Roberson C Programming 8 04-09-2008 11:09 PM
Re: #ifdef __cplusplus Philip Potter C Programming 4 04-09-2008 08:53 PM
#if (defined(__STDC__) && !defined(NO_PROTOTYPE)) || defined(__cplusplus) Oodini C Programming 1 09-27-2005 07:58 PM



Advertisments