Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top