How does an “extern C” declaration work?

Extern "C" is used to ensure that the symbols following are not mangled (decorated).

Up vote 6 down vote favorite 1 share g+ share fb share tw.

I'm taking a programming languages course and we're talking about the "extern C" declaration. How does this declaration work at a deeper level other than "it interfaces C and C++"? How does this affect the bindings that take place in the program as well?

C++ c extern link|improve this question asked Mar 8 '10 at 17:49samoz8,61453779 88% accept rate.

Extern "C" is used to ensure that the symbols following are not mangled (decorated). Example: Let's say we have the following code in a file called test. Cpp: extern "C" { int foo() { return 1; } } int bar() { return 1; } If you run gcc -c test.

Cpp -o test. O Take a look at the symbols names: 00000010 T _Z3barv 00000000 T foo foo() keeps its name.

4 Also that functions use the C calling convention, if it differs from the C++ calling convention. Which it probably doesn't, for C++ functions which take only types from C as parameters, but with extern "C" it definitely doesn't. – Steve Jessop Mar 8 '10 at 17:59.

When you flag a block of code with extern "C", you're telling the system to use C style linkage. This, mainly, affects the way the linker mangles the names. Instead of using C++ style name mangling (which is more complex to support operator overloads), you get the standard C-style naming out of the linker.

I don't think you should be suggesting that a method could be flagged with extern "C". Single functions or multiple functions are OK. – quamrana Mar 8 '10 at 18:09 @Reed: err.. I've just realised that you talk of linkage and linkers.

It affects the compiler naming, which only indirectly affects the linker. – quamrana Mar 8 '10 at 18:36 @quamrana: Read msdn.microsoft.com/en-us/library/0603949... - It does directly affect, and is used by, the linker. The compiler mangles the name, but the linker is the one that USES the naming - extern "C" is there in order to allow the linker to work properly, whether in C or C++, since the linker is the one that has to determine how to map between translation units.

– Reed Copsey Mar 8 '10 at 18:39.

In C++ the name/symbol of the functions are actually renamed to something else such that different classes/namespaces can have functions of same signatures. In C, the functions are all globally defined and no such customized renaming process is needed. To make C++ and C talk with each other, "extern C" instructs the compiler not to use the C convention.

3 for more on name mangling, there's Wikipedia: en.wikipedia.org/wiki/Name_mangling – jakebman Mar 8 '10 at 17:59 @jakebman: That is a great article. Thanks! – gilbertc Mar 8 '10 at 18:13.

Extern "C" denotes that the enclosed code uses C-style linking and name mangling. C++ uses a more complex name mangling format. Here's an example: en.wikipedia.org/wiki/Name_mangling int example(int alpha, char beta); in C: _example in C++: __Z7exampleic.

Keep in mind there is no single name mangling, it's all dependent on implementation. – GManNickG Mar 8 '10 at 19:03.

It should be noted that extern "C" also modifies the types of functions. It does not only modify things on lower levels: extern "C" typedef void (*function_ptr_t)(); void foo(); int main() { function_ptr_t fptr = &foo; } // error! The type of &foo does not equal the type that the typedef designates (although the code is accepted by some, but not all compilers).

Let's look at a typical function that can compile in both C and C++: int Add (int a, int b) { return a+b; } Now in C the function is called "_Add" internally. Whereas the C++ function is called something completely different internally using a system called name-mangling. Its basically a way to name a function so that the same function with different parameters has a different internal name.

So if Add() is defined in add. C, and you have the prototype in add. H you will get a problem if you try to include add.

H in a C++ file. Because the C++ code is looking for a function with a name different to the one in add. C you will get a linker error.

To get around that problem you must include add. C by this method: extern "C" { #include "add. H" } Now the C++ code will link with _Add instead of the C++ name mangled version.

That's one of the uses of the expression. Bottom line, if you need to compile code that is strictly C in a C++ program (via an include statement or some other means) you need to wrap it with a extern "C" { ... } declaration.

The leading _ is compiler/linker/system dependent. – Carl Norum Mar 8 '10 at 17:57 Ok, but I've never come across a C compiler that doesn't do that, even from the Amiga days :) – Cthutu Mar 9 '10 at 16:35.

Extern C affects name mangling by the C++ compiler. Its a way of getting the C++ compiler to not mangle names, or rather to mangle them in the same way that a C compiler would. This is the way it interfaces C and C++.

As an example: extern "C" void foo(int i); will allow the function to be implemented in a C module, but allow it to be called from a C++ module. The trouble comes when trying to get a C module to call a C++ function (obviously C can't use C++ classes) defined in a C++ module. The C compiler doesn't like extern "C".

So you need to use this: #ifdef __cplusplus extern "C" { #endif void foo(int i); #ifdef __cplusplus } #endif Now when this appears in a header file, both the C and C++ compilers will be happy with the declaration and it could now be defined in either a C or C++ module, and can be called by both C and C++ code.

Here is a quote from msdn "The extern keyword declares a variable or function and specifies that it has external linkage (its name is visible from files other than the one in which it's defined). When modifying a variable, extern specifies that the variable has static duration (it is allocated when the program begins and deallocated when the program ends). The variable or function may be defined in another source file, or later in the same file.

Declarations of variables and functions at file scope are external by default. " msdn.microsoft.com/en-us/library/0603949....

4 That doesn't describe what extern "C" does at all... – Carl Norum Mar 8 '10 at 17:54 It is a quote from Microsoft, And the way it has been used for years, You are going to ding me with a quote from Microsoft, I suggest you go take a c class – Joe Pitz Mar 8 '10 at 17:59 2 Being a "quote from Microsoft" doesn't make it relevant to the question at hand. It's talking about extern in other contexts. – Steve Fallows Mar 8 '10 at 18:04 4 @Joe, because it's not an answer to the question.

Extern by itself and extern "C" do different things. – Carl Norum Mar 8 '10 at 18:08 2 It says nothing about the "C" part. You can't explain extern "c" without mentioning mangling.

– Steve Fallows Mar 8 '10 at 18:09.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions