How does extern "C" work in C++? [duplicate]
Asked Answered
A

4

7

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?

Aleshia answered 8/2, 2012 at 9:17 Comment(1)
Good information on extern "c" here: #1042366Constipate
M
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.

Matta answered 8/2, 2012 at 9:20 Comment(6)
but the code I saw is:#ifdef __cplusplus extern "C" {} #endif , so is it wrong code?Aleshia
@ratzip it's not wrong, it's just dead code. It does nothing.Matta
It tells the compiler (not the linker) to use C conventions for the functions, rather than C++. This involves not only name mangling, but the conventions for passing arguments, and possibly other things as well.Tropho
@JamesKanze are you saying the linker doesn't need to know about name mangling?Matta
Not at all. The compiler mangles names. The linker joins up whatever names it's given to whatever names its's asked-for.Krissykrista
@LuchianGrigore Not necessarily. About the only time most linkers take name mangling into account is when outputting error messages.Tropho
M
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

Montpelier answered 8/2, 2012 at 9:20 Comment(1)
+1 you said in one line what I was going to say in 100. Well done.Meadors
M
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.

Matta answered 8/2, 2012 at 9:20 Comment(6)
but the code I saw is:#ifdef __cplusplus extern "C" {} #endif , so is it wrong code?Aleshia
@ratzip it's not wrong, it's just dead code. It does nothing.Matta
It tells the compiler (not the linker) to use C conventions for the functions, rather than C++. This involves not only name mangling, but the conventions for passing arguments, and possibly other things as well.Tropho
@JamesKanze are you saying the linker doesn't need to know about name mangling?Matta
Not at all. The compiler mangles names. The linker joins up whatever names it's given to whatever names its's asked-for.Krissykrista
@LuchianGrigore Not necessarily. About the only time most linkers take name mangling into account is when outputting error messages.Tropho
F
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.

Fransen answered 8/2, 2012 at 9:19 Comment(1)
@Nawaz: And who said so? The "C"is.Fransen
A
0

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

Aureliaaurelian answered 8/2, 2012 at 9:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.