12

I'm not asking about WHEN to link different programming langauges.

This is quite a general question but I'm personally working on Linux.

What I want to understand is the process by which different programming languages can be combined, I found a good article on combining C/C++/Fortran: http://www-h.eng.cam.ac.uk/help/tpl/languages/mixinglanguages.html.

From what I understand most compilers perform two stages:

  1. Translating the language files into object files which contain machine code but still contain some symbols (possibly function names?)

  2. Linking the object files together, only at this stage the Linker checks that the functions in the object files are callable.

I think that the problem with combining different languages is name mangling which means that the names of the functions are changed when they are turned into object code.

The questions are:

  1. Can't you somehow discover the mangled function names beforehand and than specify them explicitly in the programming language or better off, isn't there a software that already does that?

  2. I don't understand completely how dynamic libraries are linked but can different languages interact by the same method that programs interact with dynamic libraries?

p.s The main intent is to call functions written in another language.

4 Answers 4

10

The issue with linking different object files together generally comes down to subroutine calling conventions. Basically, when you make a call to a routine located in another object file, your compiler will have to know what that other object file will name its routine internally, how to pass all its parameters, and what (if any) setup and cleanup code the routine will require. All this stuff is generally grouped together under the heading of calling conventions.

Each compiler has its own calling conventions it likes to use for subroutines. Note I said "compiler", not language. The C calling convention in Linux is different than the C calling convention on Windows.

So when you mix languages, you need some way to tell the compiler for either the calling or the called subroutine to use the other language's calling convention. C's convention is a popular one to use as sort of a "lingua franca", as just about every platform has a C compiler. However some platforms (eg: Windows) have multiple popular calling conventions.

So now we ask the question you asked in the comments:

Is there a common way to "tell the compiler to use the other language's calling convention"?

And the answer is, "No, not really". Some languages do have defined ways of using specific other language's calling conventions. For example, C++ allows you to to put extern "C" on declarations to tell the compiler that the declaration(s) in question use the C calling convention. Ada accomplishes the same thing with pragma Convention (X,...), where X is the convention name. C, Fortran, and Cobol are defined by the language, but anything else supported (eg: Windows' Stdcall) is implementation defined.

However, if you have a pair of languages whose compiler writers never thought of each other, then you have no choice but to tell both to use some third convention that they both know about (usually C's). For example, to get standard C++ and Ada to interoperate, you'd have the server code export its routines using the C convention, and tell the client code that the routines it is calling are using the C convention.

3
  • 1
    Thanx a lot for the answer, is there a common way to "tell the compiler to use the other language's calling convention"?
    – fiftyeight
    Jul 20, 2011 at 20:04
  • 1
    This is a great answer, to clarify, I'd be grateful if you could tell me if the following is correct: Only the ABI/Calling Convention matter as to whether you can link together different objects, if I want to have a high-level language like Perl or PHP (which are written in C) be able to call my function than as long as the function uses the C ABI, it will work. I just need to add the shared object to the configuration file (like php.ini) or link it in some other way.
    – fiftyeight
    Jul 20, 2011 at 22:57
  • 2
    @fiftyeight, for Perl, take a look at Inline::C. As long as you use the C ABI, there will be a way or another to make it work for a given scripting language in most cases. The ways to make it work will depend on the details of the situation. But note that most scripting languages are written in C. For the ones implemented on top of the JVM, you'll have to resort to the JVM's facilities for native code interop (JNI and whatnot).
    – Lumi
    Jul 21, 2011 at 18:04
1

Different languages can definitely use the same libraries. On the old Windows Visual Basic it was quite common to dynamically load Windows API functions, for instance.

All you need for inter-language linking is an agreement on the function's calling conventions, along with knowledge of the function names. The former has to be done by looking up the documentation; the latter has to be looked up in the compiler that created the objects or libraries. For example, gcc will compile C without mangling names, so you can refer directly to the function names as they are in your C source, while g++ will compile C++ code with mangled names and you're best off exposing C functions via extern "C" declarations.

Basically, as long as your objects or libraries expose only the C ABI, there should be widespread support for binding to other languages. It's a lot more difficult if you want to use a native C++ library, for instance, since in that case your foreign languages have to implement the correct C++ ABI. It's similar for exporting code from, say, Fortran, but I believe that one can be made to just uses the C ABI.

5
  • I don't quite understand the last part, what do you mean by "your objects or libraries expose only the C ABI"
    – fiftyeight
    Jul 20, 2011 at 20:01
  • Well, your libraries could be using all sorts of different internal things (like C++ stuff) that you couldn't readily make sense of externally, but as long as the functions that you want to expose to others follow the C ABI, other programs can link them dynamically.
    – Kerrek SB
    Jul 20, 2011 at 20:03
  • How can I know if the compiler/language I use is compatible with the C ABI, is there a way to know this?
    – fiftyeight
    Jul 20, 2011 at 20:07
  • You'll have to check the compiler documentation. I think GCC does the right thing for all C functions, for C++ functions with extern "C", and probably (though I don't know) for Fortran, and you can probably get the other GCC frontend languages to emit C-style functions, too.
    – Kerrek SB
    Jul 20, 2011 at 20:23
  • This is a great answer, to clarify, I'd be grateful if you could tell me if the following is correct: Only the ABI/Calling Convention matter as to whether you can link together different objects, if I want to have a high-level language like Perl or PHP (which are written in C) be able to call my function than as long as the function uses the C ABI, it will work. I just need to add the shared object to the configuration file (like php.ini) or link it in some other way
    – fiftyeight
    Jul 20, 2011 at 22:57
0

The "standard" is to use non-mangled names when combining programs from different languages. Name mangling can be turned off for specific symbols in C++ by declaring them with extern "C". C does not mangle names.

0

All library executables contain some type of interface. If they did not, no software would be able to work with them. It is more likely internal methods get changed to be more efficient. In addition, many languages allow you to turn off "mangling" at the compiler level.

Linking, as a simple explanation (I will probably get dinked for this?), is packaging into a single file. The classes retain the same interface as non-linked libraries, at least from an external programming standpoint.

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.