The C++ language allows intermixing of both C++ and C in one source file. For example,
extern "C" {
struct bar { /* ... */ }
}
Does C++ or has it ever supported any other “linkage languages” other than C? e.g. extern "Pascal"
or extern "Haskell"
3
extern "C"
may be the only required one:
Every implementation shall provide for linkage to functions written in the C programming language, “C”, and linkage to C++ functions, “C++”.
However others can exist as per this quote:
The string-literal indicates the required language linkage. This International Standard specifies the semantics for the string-literals “C” and “C++”. Use of a string-literal other than “C” or “C++” is conditionally- supported, with implementation-defined semantics. [ Note: Therefore, a linkage-specification with a string- literal that is unknown to the implementation requires a diagnostic. — end note ] [ Note: It is recommended that the spelling of the string-literal be taken from the document defining that language. For example, Ada (not ADA) and Fortran or FORTRAN, depending on the vintage. — end note ]
2
In many (most) environments “extern “C” is the default linking convention for any language on the system.
So depending on the operating system and the implementation of the “foreign” language “extern C” is all you need to call a program in another compiled language.
3
G++ at some point supported extern "Java"
to interact with GCJ-compiled Java code.
https://stackoverflow.com/questions/15167867/extern-java-block-in-gcc
-
No! C++ only support C as a “linkage languages”.
-
extern “C” {
struct bar { /* … */ }
} -
The syntax as above means that do not change the name of variable,struct or function. Because C++ maybe change the name of variable,struct or function to implement Class ,Namespace and function overloading.
-
If you do not do this,the linker maybe can not find out the object of variable,struct of function.
-
C++ is designed to be compatible with C. The extern “C” is the way to implement compatible with C.
1