9

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?

4
  • No, because almost everything else is compatible with C and its calling conventions. Commented Jan 31, 2014 at 16:04
  • 2
    There's extern pascal as well... Commented Jan 31, 2014 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. Commented Jan 31, 2014 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. Commented Apr 30, 2016 at 19:04

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

10 Comments

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.
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.
@PaulR Well, we are going back a little now!
Well it seems like only yesterday to me... ;-)
IBM zOS compiler documents extern "FORTRAN", extern "COBOL", and even extern "PLI"
|
0

Most contemporary C++ compilers don't support anything other the extern "C" and extern "C++" required by the ISO C++ standard. Integration with other languages most commonly uses C calling conventions and extern "C".

And even when non-C calling conventions were supported, they often weren't selected by an extern string literal, they still used extern "C" but in conjunction with some non-standard calling convention declaration: for example, Microsoft C++ for 16-bit platforms (DOS, Windows 3.x, OS/2 1.x) used __pascal for the Pascal calling convention and __fortran for Fortran, instead of something like extern "Pascal" or extern "Fortran".

That said, IBM's contemporary C++ compilers for z/OS support extern "FORTRAN" for calling Fortran, extern "COBOL" for calling COBOL, and extern "PLI" for calling PL/I.

Similarly, IBM's ILE C++ compiler on IBM i supports a whole list of different extern string literals, see PDF page 338 of IBM i: ILE C/C++ Programmer's Guide, like extern "RPG", extern "C nowiden" (which is like extern "C", but turns off integer promotion, so a short will actually be passed as a 2 bytes instead of 4), extern "OS" (which enables IBM's legacy pre-ILE calling convention).

It appears, nowadays, IBM is the only major C++ vendor who supports non-"C"/"C++" extern, and even for them only on their mainframe and midrange platforms; but, historically, there probably were others.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.