8

As it is known, declaring extern "C" to C++ function makes its name have C linkage, enabling C code to link.

My question is - are there other programming languages we can make C++ function names have linkage to, something like extern "Lisp" or extern "FORTRAN"?

If not, why? What is the internal structure behind the "C", that makes the limitations?

What are the alternatives?

Reflection
  • 1,936
  • 3
  • 21
  • 39
  • No, because almost everything else is compatible with C and its calling conventions. – Some programmer dude Jan 31 '14 at 16:04
  • 2
    There's extern pascal as well... – Almo Jan 31 '14 at 16:05
  • 1
    "C" is the only such linkage *required by the C++ standard*, but compilers are free to provide more; that's why it's a string constant. extern "FORTRAN" would make a lot of sense for compilers that have both C++ and FORTRAN front ends. I wouldn't expect to see extern "Lisp" if only because a Lisp environment tends to be its own monolith. – zwol Jan 31 '14 at 16:10
  • language linkage (extern "C++", extern "C", extern "FORTRAN") is not the same thing as calling convention (stdcall, fastcall, etc), which this was marked as a duplicate for. – Cubbi Apr 30 '16 at 19:04

1 Answers1

4

The C++ standard, 7.5.2 dcl.link, says:

Linkage between C++ and non-C++ code fragments can be achieved using a linkage-specification:

linkage-specification:
    extern string-literal { declaration-seqopt}
    extern string-literal declaration

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 ]

So in principle, implementers can choose to support other linkage specifications than C and C++.

In practise however, on all modern platforms, C linkage is the lowest common denominator. As a general rule, these days, binary interop for languages other than C uses C linkage. On the widely used modern platforms, you will not see anything other than C and C++ linkage.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 1
    `extern "Pascal"` exists (although it's a non-standard extension) - it is used to differentiate between the different parameter passing conventions between C and Pascal. – Paul R Jan 31 '14 at 16:09
  • @PaulR It's not been used for a very long time (none of my compilers support it, I think), and never by me. And I've done a lot of C/Pascal interop. These days all Pascal compilers can do `cdecl` and `stdcall`. – David Heffernan Jan 31 '14 at 16:12
  • Rummaging Google shows that `extern "FORTRAN"` also exists. However, I could not find a reference formally settled the issue - anyone have? – Reflection Jan 31 '14 at 16:13
  • 3
    It used to be common on "classic" Mac OS compilers in the 1980s and 1990s, during the transition from Pascal to C/C++ as the main development languages for the Mac. The Mac toolbox and OS used Pascal calling conventions for historical reasons. – Paul R Jan 31 '14 at 16:13
  • @Reflection Try finding a compiler that will compile anything other than `extern "C"`. – David Heffernan Jan 31 '14 at 16:14
  • @PaulR Well, we are going back a little now! – David Heffernan Jan 31 '14 at 16:15
  • 1
    Well it seems like only yesterday to me... ;-) – Paul R Jan 31 '14 at 16:15
  • 2
    IBM zOS compiler documents extern "FORTRAN", extern "COBOL", and even extern "PLI" – Cubbi Apr 30 '16 at 19:16