Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Re: #ifdef __cplusplus

 
 
Philip Potter
Guest
Posts: n/a
 
      04-09-2008
mattia wrote:
> 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
 
Reply With Quote
 
 
 
 
Richard
Guest
Posts: n/a
 
      04-09-2008
Philip Potter <> writes:

> mattia wrote:
>> 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.
 
Reply With Quote
 
 
 
 
Philip Potter
Guest
Posts: n/a
 
      04-09-2008
Richard wrote:
> Philip Potter <> writes:
>
>> mattia wrote:
>>> 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.


It's up to the OP. __cplusplus doesn't have much meaning in C, and
extern "C" even less, so if you want to be sure of your answers, you're
better asking a C++ expert.

Phil
 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      04-09-2008
Philip Potter <> writes:

> Richard wrote:
>> Philip Potter <> writes:
>>
>>> mattia wrote:
>>>> 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.

>
> It's up to the OP. __cplusplus doesn't have much meaning in C, and
> extern "C" even less, so if you want to be sure of your answers, you're
> better asking a C++ expert.
>
> Phil


As I said - the answer was already posted many times. There are many
here who know both - and it is a "both" question since many C
programmers mix and match.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      04-09-2008
mattia wrote:
>
> I think that I'll note use __cplusplus unless I'll have to link my
> headers with cpp code. That will be my rule. Thanks all for the reply.
>

If you think there's the remotest chance your C library might be used
with C++, you may as well use the "extern C" wrappers. There's no
cost/impact to C code and you might save you or your users future work.

I always use "extern C" wrappers.

--
Ian Collins.
 
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 Antoninus Twink C Programming 5 04-09-2008 08:50 PM
#if (defined(__STDC__) && !defined(NO_PROTOTYPE)) || defined(__cplusplus) Oodini C Programming 1 09-27-2005 07:58 PM



Advertisments