How to combine different programming languages

I am not asking about WHEN to link various programming language programs.

This is a pretty general question, but I personally work on Linux.

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

From what I understand, most compilers do two steps:

  • Translation of language files into object files containing machine code, but still contain some characters (maybe function names?)

  • Linking object files together, only at this point Linker verifies that functions in object files can be called.

I think the problem with combining different languages ​​is the name, which means that the names of the functions change when they turn into object code.

Questions:

  • Could you somehow first detect the changed function names and not indicate them explicitly in a programming language or better, is there no software that already does this?

  • I don’t fully understand how dynamic libraries are linked, but can different languages ​​interact in the same way that programs interact with dynamic libraries?

ps The main goal is to call functions written in another language.

+6
source share
4 answers

The problem with linking different object files together, as a rule, comes down to calling subroutines. In principle, when you make a call to a routine located in another object file, your compiler will need to know what this other object file will call its internal procedure, how to transfer all its parameters, and what (if any) setup and cleaning code will require routine. All of these materials are usually grouped under the heading of a calling agreement .

Each compiler has its own calling conventions, which he likes to use for routines. Notice, I said "compiler", not a language. The C calling convention on Linux is different from the C calling convention on Windows.

Therefore, when you mix languages, you need to somehow tell the compiler that the calling convention of the other language is used for the calling or called routine. The C convention is popular for use as a kind of "lingua franca", since almost every platform has a C compiler. However, on some platforms (for example: Windows) there are several popular calling conventions.

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

Is there a common way to "tell the compiler to use a convention about calling another language"?

And the answer: "No, not really." Some languages ​​have identified ways to use special conventions for other languages. For example, C ++ allows you to put extern "C" in declarations to tell the compiler that the declarations in use are using the C calling convention. Ada does the same with pragma Convention (X,...) , where X is the name of the convention. C , Fortran and Cobol defined by the language, but anything that is supported (for example: Windows' Stdcall ) is determined by the implementation.

However, if you have a couple of languages ​​whose compilers never thought of each other, then you have no choice but to say that you need to use the third convention, which they both know (usually C). For example, to interact with standard C ++ and Ada, you must have server code exporting your routines using the C convention and tell the client code that the routines it invokes use the C convention.

+6
source

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

All you need for an interlanguage connection is an agreement on function call conventions, as well as knowledge of function names. The first should be done by looking for documentation; the latter must be sought in the compiler that created the objects or libraries. For example, gcc will compile C without distorting names, so you can refer directly to function names as they are in your C source, while g++ will compile C ++ code with malformed names, and you better expose C functions through extern "C" declarations extern "C" .

Basically, as long as your objects or libraries expose only C ABI, support for binding to other languages ​​should be widespread. This is much more complicated if you want to use your own C ++ library, for example, since in this case your foreign languages ​​must implement the correct C ++ ABI. This is similar to exporting code, say, from Fortran, but I believe that you can just use C ABI.

+1
source

"Standard" - use undamaged names when combining programs from different languages. Name calling can be disabled for certain characters in C ++ by declaring them with extern "C" . C does not change names.

0
source

All library executables contain some type of interface. If they did not, no software will be able to work with them. Rather, internal methods are becoming more efficient. In addition, many languages ​​allow you to disable "manipulation" at the compiler level.

Binding, as a simple explanation (will I probably take it?), Is packaged into a single file. Classes retain the same interface as unrelated libraries, at least in terms of external programming.

0
source

Source: https://habr.com/ru/post/893206/


All Articles