Various programming languages ​​and convention

According to the wiki:

Different programming languages ​​use different calling conventions and therefore there may be different platforms (CPU architecture + operating system). This can sometimes cause problems when combining modules written in several languages.

How should I be careful when I call C / C ++ functions (exported from .so / .dll) in Python? If so, what should I observe?

+4
source share
4 answers

The calling convention is not a problem when calling C from python, because the python interpreter itself is written in C and thus uses the same calling convention as the C code.

This becomes a problem when combining code written in different compiled languages, such as C and Ada.

Typically, C is a system programming language and, therefore, a standard. This means that a compiler for any language will usually have special support for interacting with C. See here for an example of the interaction between C and Ada. If there is no special support, wrappers should be written at the assembly level.

If you need to call C ++ / Ada from python, you will need to perform a two-step process. The first step is to write a C wrapper around C ++ / Ada functions. The second step is to call the C shell from python.

Take, for example, the following C ++ class.

class Foo { public: Foo (); int bar (); ... }; 

If you want to make it available to python, you need to wrap it in C first:

 extern "C" { typedef void *FooPtr; FooPtr foo_new () { return (FooPtr)new Foo(); } int foo_bar (FooPtr foo) { return ((Foo*)foo)->bar(); } ... } 

Then you can call this from python. (Note: in real life there are tools to automate this, such as Boost.Python ).

Note that there are two aspects to writing a wrapper: conversion between conventions between calls and conversion between type representations. The later part, as a rule, is the most difficult, because as soon as you go beyond the framework of basic types, languages ​​tend to have very different representations of equivalent types.

+4
source

The call between Python and C is a solvable problem, so you don’t have to worry about anything at all - not least because Python is written in C.

The problem described here is a big problem when all languages ​​on the platform are developed independently, individually loaded from assembler. For example, usually there were problems causing between C and FORTRAN, as well as between C and Pascal at a time when all three of these languages ​​coexisted as gross equal. The old Mac Toolbox was written primarily in assembly language using Pascal calling conventions, and early application developers used Borland Pascal. But then C compilers, such as Symatec THINK C, appeared, and these programmers had to worry especially about how to translate argument types and line conventions (Pascal lines carry length bytes at the beginning, and, of course, C lines have 0 at the end.)

+5
source

C and C ++ have different calling conventions. C does not support C ++ function calls, but C ++ supports C function calls and supports the implementation of C functions in C ++. That is, you can declare functions as extern "C" in C ++. I don't know how python bindings call C ++, but you have to be careful to call C and C ++ functions with their corresponding calling conventions. Typically, C calling conventions are used when calling between different languages. This may mean that you may need to write a wrapper function using C style conventions for C ++ style functions.

0
source

Invoking conventions relate to how function calls are generated in the resulting assembly. One of the conventions is that the caller is responsible for creating and clearing the stack space for local variables. Another convention is that the called function creates and clears the stack space for local variables. It doesn't matter which one you use, but obviously they must match or the stack will get corrupted.

Here you can see more detailed information: http://en.wikipedia.org/wiki/Calling_convention

0
source

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


All Articles