How compatibility works

I know that many large-scale applications, such as video games, are created using several languages. For example, it is likely that the game / physical engines are written in C ++, while the tasks of the gameplay, the graphical interface is written in the form of Python or Lua.

I understand why this separation of roles is performed; use lower-level languages ​​for tasks requiring extreme optimization, tuning, efficiency and speed, when using higher-level languages ​​to speed up production time, reduce unpleasant errors, etc.

Recently, I decided to take on a larger personal project and would like to separate parts of the project like the ones above. At the moment, I am very confused about how this compatibility between languages ​​(especially compiled and interpreted) works.

I am well acquainted with the details of moving from an ANSCII code test to loading an executable file when it is written as something like C / C ++. I am very curious about how a video game built in different languages ​​works. This is a big / wide question, but I'm particularly interested in:

  • How does code level logic work? That is, how can I name Python code from a C ++ program? Moreover, they do not support the same built-in types?
  • What does the program image look like? From what I can say, a video game works in one process, and what does the image look like at runtime when starting a C / C ++ program that calls a Python function?
  • If you call code from an interpreted language from a compiled program, what is the sequence of events that occur? Ie If I am inside my compiled executable and for some reason called the interpreted language inside the loop, do I need to wait for the interpreter to be at each iteration?

I find it difficult to find information about what is happening at the machine level, so any help would be appreciated. Although I am generally curious about the interaction of software, I am particularly interested in the interaction of C ++ and Python.

Thank you for your understanding, even if it just tells me where I can find more information.

+6
source share
2 answers

In the specific case of python, you have basically three options (and this usually applies in all directions):

  • Host python in C ++: from the point of view of a C ++ program, the python interpreter is a C library. On the python side, you may or may not need to use something like ctypes to open the C (++) api.

  • Python uses C ++ code as DLL / SO libraries - C ++ code probably knows nothing about python, python should definitely use an external function interface.

  • Interprocess communication - basically, two separate processes are executed, and they talk on the socket. These days you are probably using some kind of web service architecture to accomplish this.

+2
source

Depending on what you want to do:

  • Take a look at SWIG: http://www.swig.org/ This is a tool that is designed to connect C / C ++ code with Python, Tcl, Perl, Ruby, etc. A common use case is the Python interface (graphical or not) that will invoke C / C ++ code. SWIG will analyze C / C ++ code to create interfaces.

  • Libpython: this is the lib that allows you to embed Python code. You have some examples here: http://docs.python.org/3.0/extending/embedding.html

0
source

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


All Articles