• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: why use extern "C"
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: why use extern "C"


  • Subject: Re: why use extern "C"
  • From: Aandi Inston <email@hidden>
  • Date: Tue, 07 Mar 2017 17:42:52 +0000

This is common practice. Here's why.  In C++ this

#ifdef __cplusplus
extern "C" {
#endif
………./*some function,struct*/

#ifdef __cplusplus
}
#endif

compiles as
extern "C" {
………./*some function,struct*/
}

As you identify, this declares C linkage, usually to a precompiled C
library, or to make a C-compatible library. The extern must be conditional,
because in pure C this form of extern is a syntax error.

So what happens in pure C? The #ifdef section is skipped because
__cplusplus is undefined. So it just compiles as
………./*some function,struct*/
In other words, just C definitions, which will have C linkage by default.

This allows a header file for a C library to be compiled in C, and to be
compiled in C++ with suitable linkage.

(You should not see // in this section, because in pure C it is forbidden).



On 7 March 2017 at 16:45, bigpig <email@hidden> wrote:

> I see some code like this in iOS project:
>
> #ifdef __cplusplus
> extern "C" {
> #endif
>
> ……….//some function,struct
>
>
> #ifdef __cplusplus
> }
> #endif
> if there is C++ compiler and use C linkage,but if there isn’t C++ compiler
> then use what?
> And what is the reason of using this way in code?
>
> Thanks!
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden


  • Follow-Ups:
    • Re: why use extern "C"
      • From: Charles Srstka <email@hidden>
References: 
 >why use extern "C" (From: bigpig <email@hidden>)

  • Prev by Date: why use extern "C"
  • Next by Date: Re: why use extern "C"
  • Previous by thread: why use extern "C"
  • Next by thread: Re: why use extern "C"
  • Index(es):
    • Date
    • Thread