In this article the keyword extern can be followed by "C" or "C++". Why would you use 'extern "C++"'? Is it practical?
11 Answers
The language permits:
extern "C" {
#include "foo.h"
}
What if foo.h contains something which requires C++ linkage?
void f_plain(const char *);
extern "C++" void f_fancy(const std::string &);
That's how you keep the linker happy.

- 13,614
- 6
- 40
- 51
-
4I do not understand the downvotes. This is a use case where extern C++ is required, and it is legal. – Thomas L Holaday Mar 04 '09 at 15:16
-
1i don't understand it either. it's a valid use of extern "C++" – Johannes Schaub - litb Mar 04 '09 at 15:19
-
3Not only is it valid, I've seen it in use in the wild (it was my 'aha' moment for it). Take a look at winnt.h. – Michael Burr Mar 04 '09 at 15:26
-
Don't worry; I voted it up because of the downvotes when I otherwise wouldn't have. Not because it doesn't deserve to be voted up, but simply because I wouldn't have thought to otherwise. – flarn2006 Oct 04 '13 at 08:36
There is no real reason to use extern "C++"
. It merely make explicit the linkage that is the implicit default. If you have a class where some members have extern "C" linkage, you may wish the explicit state that the others are extern "C++".
Note that the C++ Standard defines syntactically extern "anystring"
. It only give formal meanings to extern "C"
and extern "C++"
. A compiler vendor is free to define extern "Pascal"
or even extern "COM+"
if they like.

- 101,701
- 37
- 181
- 258
-
This is really the perfect answer to the question. I would give more votes if I could. – Randolpho Mar 04 '09 at 15:19
-
4You use it when you need to override some other linkage specification in a surrounding scope. – Thomas L Holaday Mar 04 '09 at 15:51
-
8
I'm not sure why you would need to do it, but according to this article from Sun, you can use extern "C++" inside a block of extern "C" to specify certain functions in a group of "C" functions have the native C++ linkage.
extern "C" {
void f(); // C linkage
extern "C++" {
void g(); // C++ linkage
extern "C" void h(); // C linkage
void g2(); // C++ linkage
}
extern "C++" void k();// C++ linkage
void m(); // C linkage
}

- 3,120
- 1
- 34
- 57
-
This is probably to deal with situations where a header is included within one of the extern sections. – ThomasW Jun 14 '11 at 11:29
Two guesses:
- If you are in a
extern "C"
block, you can get C++ language linkage again by specifying a nestedextern "C++"
. - It reserves
C++
linkage, because it's the document defining C++. Who is in a better position for definingC++
language linkage than it itself. It also provides for completeness. Same deal as withsigned/unsigned
.
Read this answer that explains extern "LanguageName"
(i.e GCC has extern "Java"
) aswell.

- 1
- 1

- 496,577
- 130
- 894
- 1,212
Extern "C" is answered by many. The use case for extern "C++" is when calling C++ library function in a C function. The sub-use case, that is relevant, is when linking a C++ library with a C source code with main function. Check this wiki page for more details:

- 77,474
- 47
- 185
- 261

- 41
- 2
C and C++ use different name mangling rules. Essentially, extern "C" tells the C++ compiler to name the function as C would name it.

- 23,810
- 2
- 71
- 76
-
3Actually, name mangling is purely an implementation detail. The languages themselves have no mangling "rules" at all. Various compilers have different ways handling function overloading. Name mangling is just one popular method. – James Curran Mar 04 '09 at 15:17
This specify which link convention to use. Most languages know how to link with a "C" style function.
You need this in two cases :
- A C - or other languages for that matter- program calling a function written in C++
- A C++ program calling a function written in C
Example :
// declared in function.h
void f1(void);
Your C code - actually other languages are able to link with C function - will not be able to link to it because the name in the object table will use C++ convention.
If you write
extern "C" void f1(void);
Now the linking works because it uses C convention.

- 6,100
- 26
- 31
The #1 reason I use extern "C" is to avoid C++'s name mangling rules. This is very important if you are working in a .Net language and want to PInvoke into a particular native function. The only way to do this is with name mangling disabled.

- 733,204
- 149
- 1,241
- 1,454
To answer the second question, "is it practical?":
It is practical, and practically unavoidable, in standard headers like <cmath>
.
Imagine a header-only library X.h, written in a common subset of C++ and C, and intended to be used from both languages. For the benefit of C++ users, X.h includes <cmath>
, not <math.h>
. This would not work for C users though, hadn't the authors of <cmath>
sandwiched everything in extern "C++" {
... }
.

- 7,290
- 5
- 31
- 59
Short answer is that you can use extern C to tell the compiler not to use name-mangling. This means you can link together bits of C and C++ code in the same project.

- 98,941
- 38
- 226
- 299

- 4,135
- 4
- 26
- 25
-
But, the question body also asks the question that James answered. – Dominic Rodger Mar 04 '09 at 15:06
-
1extern "c" is easily googled, but extern "c++" is an interesting question, will take it back if I'm wrong! – Paul Dixon Mar 04 '09 at 15:09
-
It seemed to me to be a question about why you would use either. There are better answers now so it doesn't matter. – James Brooks Mar 04 '09 at 15:15
extern "C" is used to say that a C++ function should have C linkage. What this means is implementation dependant, but normally it turns off C++ name-mangling (and so overloading and strict type checking). You use it when you have a C++ function you want to be called from C code:
extern "C" void Foo(); // can be called easily from C
As for extern "C++", I've never seen it in real code, though the C++ Standard allows it. I guess it is a no-op.

- 26,130
- 9
- 42
- 54
-
1Actually, it's mostly used for the reverse: calling a C function from C++ code. If you call `printf` in your C++ code, the link needs to know that it's in the library as `printf` and not `printf_#$D%G^_&_^` – James Curran Mar 04 '09 at 15:49