25

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
  • most of the time both languages will do their extern "C" equivalent to interact with each other Aug 23, 2013 at 0:45
  • C++ supports other linkage by allowing imlementations to add their linkage types (§7.5), the question should be "Do implementations exist or have implementtions existed" ;-)
    – johannes
    Aug 23, 2013 at 2:25
  • 2
    extern "C" only affects linkage, not the syntax and semantics of the text inside the block. In other words, it's still C++ inside that extern "C" block; the symbol names just aren't mangled. See stackoverflow.com/q/1041866/712605 Aug 23, 2013 at 19:10

4 Answers 4

17

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
  • 1
    Where is this quoted from?
    – paul
    Aug 28, 2013 at 13:42
  • @paul c++11 standard draft
    – aaronman
    Aug 28, 2013 at 14:29
9

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
  • Even environments not using C linkage (Pascal i.e. often uses other argument order on stack and the callee has to free the value) they have a C-compatible mode
    – johannes
    Aug 23, 2013 at 2:28
  • @johannes: Actually Windows use Pascal convention for many things, but since it needs that in C too and C does not have the "extern "Language"" syntax, they do it via platform-specific C extension, so for C++ it's still just extern "C" and the the special keyword specifies the different calling convention.
    – Jan Hudec
    Aug 23, 2013 at 11:59
  • 1
    Still there might be cases where a compiler vendor adds special linkage types. Besides interoperability with other languages this might also be interesting to interoperability with different C++ name mangling. Question is whether anybody did :)
    – johannes
    Aug 23, 2013 at 12:27
7

G++ at some point supported extern "Java" to interact with GCJ-compiled Java code.

https://stackoverflow.com/questions/15167867/extern-java-block-in-gcc

-4
  • 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
  • 11
    Please check §7.5 of the C++ standard. extern "C" and extern "C++" are the only mandatory ones, but implementations are allowed to provide others.
    – johannes
    Aug 23, 2013 at 2:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.