6

I see some code in C++ using extern "C" at the beginning of the file like this:

#ifdef __cplusplus 
extern "C" {} 
#endif

What does this mean? How does it work?

Jonathan Leffler
  • 698,132
  • 130
  • 858
  • 1,229
  • 1
    Good information on extern "c" here: http://stackoverflow.com/questions/1041866/what-does-extern-c-in-c-source – Chen Harel Feb 08 '12 at 09:22

4 Answers4

5

It is used to inform the compiler to disable C++ name mangling for the functions defined within the braces. http://en.wikipedia.org/wiki/Name_mangling

ksming
  • 1,382
  • 1
  • 15
  • 25
4

It's probably not like that, but more like:

#ifdef __cplusplus 
extern "C" {
#endif

//some includes or declarations

#ifdef __cplusplus 
}
#endif

It tells the compiler to use C name mangling for whatever is declared inside the directives.

The way you have it now:

#ifdef __cplusplus 
extern "C" {} 
#endif

is just dead code.

Luchian Grigore
  • 245,575
  • 61
  • 446
  • 609
0

It specifies a linkage specification.
It tells the linker how to link the code.

It is useful when you want to mix C and C++ code.

Jonathan Leffler
  • 698,132
  • 130
  • 858
  • 1,229
Alok Save
  • 196,531
  • 48
  • 417
  • 525
0

Extern "C" - notify the compiler,that the noted function is compiled in C style.

Oyeme
  • 10,830
  • 4
  • 38
  • 63