Why would you use "extern" C ++ ""?

In this article , the extern keyword may be followed by "C" or "C ++". Why would you use "extern" C ++ ""? Is it practical?

+60
c ++ c
Mar 04 '09 at 14:59
source share
10 answers

Language allows:

extern "C" { #include "foo.h" } 

What if foo.h contains something that requires C ++ communication?

  void f_plain(const char *); extern "C++" void f_fancy(const std::string &); 

The way you keep the linker happy.

+88
Mar 04 '09 at 15:02
source share

There is no real reason to use extern "C++" . It simply expresses a relationship, which is an implicit default. If you have a class in which some members have an external "C" link, you can wish for the explicit state for the rest to be extern "C ++".

Note that the C ++ standard defines syntactically extern "anystring" . It gives only formal values extern "C" and extern "C++" . The compiler provider is free to define extern "Pascal" or even extern "COM+" if one likes it.

+28
Mar 04 '09 at 15:10
source share

I'm not sure why you need this, but according to this article from Sun, you can use extern "C ++" inside the extern "C" block to indicate some functions in group "C" functions have a native C ++ connection.

 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 } 
+18
Mar 04 '09 at 15:20
source share

Two guesses:

  • If you are in the extern "C" block, you can again get a link to the C ++ language by specifying the nested extern "C++" .
  • It reserves a C++ connection because it is a document defining C ++. Who is in a better position to define C++ language bindings than himself. It also provides completeness. Same as with signed/unsigned .

Read this answer explaining extern "LanguageName" (for example, GCC has extern "Java" ).

+5
Mar 04 '09 at 15:16
source share

External "C" was answered by many. The use case for extern "C ++" is to call the C ++ library function in function C. The case of sub-use that matters is linking the C ++ library with the C source code to the main function. More about this wiki page :

+3
Nov 12
source share

C and C ++ use different rules for determining the name . Essentially, extern "C" tells the C ++ compiler to name the function, as C will call it.

+2
Mar 04 '09 at 15:03
source share

This indicates which link agreement to use. Most languages โ€‹โ€‹know how to make a connection with a style C function.

You need this in two cases:

  • C - or other languages โ€‹โ€‹for this matter - a program that calls a function written in C ++
  • C ++ program calling a function written in C

Example:

 // declared in function.h void f1(void); 

Your C code - in fact other languages โ€‹โ€‹can refer to the C function - will not be able to reference it, because the name in the object table will use the C ++ convention.

If you write

 extern "C" void f1(void); 

The link now works because it uses the C convention.

+1
Mar 04 '09 at 15:11
source share

The short answer is that you can use extern C to tell the compiler not to use the name. This means that you can link the bits of the C and C ++ code in one project.

0
Mar 04 '09 at 15:03
source share

extern "C" is used to say that a C ++ function must have a C link. This means that it is implementation dependent, but nptmally disables C ++ manipulation (and therefore overloading and strict type checking). You use it when you have a C ++ function that you want to call from C code:

 extern "C" void Foo(); // can be called easily from C 

As for extern "C ++", I have never seen it in real code, although it allows C ++ Standard. I think this is non-op.

0
Mar 04 '09 at 15:09
source share

Reason # 1 used by extern "C" is to avoid the C ++ name change rules. This is very important if you work in the .Net language and want PInvoke in a specific native function. The only way to do this is by disabling the name.

0
Mar 04 '09 at 15:17
source share



All Articles