C ++ instance instance memory class again

I know this question was asked earlier, but before you give me a minus and tell me a second question, think about this:

In all previous answers, everyone says that the layout of the object's memory depends on the compiler. How, then, that shared libraries (* .dll, * .so) can export and import C ++ classes, and they can definitely be combined, even if they come from different compilers? Consider a DirectX application written under control. DirectX was compiled using MSVC ++, so how do these environments agree with the memory layout? I know that DirectX is highly dependent on C ++ classes and polymorphism.

Ask differently: let's say that I have the selected architecture (for example, Windows, intel x86), and I'm trying to write a new compiler. How to find out how to access the class instance (vtable, member fields) provided by the .dll lib compiled by another compiler? Or is it just like this: M $ wrote VC ++, and since then it is an unwritten standard, and every other compiler does the same โ€œfor compatibility reasonsโ€? What about Linux or other OS?

EDIT:

OK I admit that the DirectX example was bad due to the COM specification ...

Another example: QT. I use QT with mingw, but I know that there are also versions of MSVC. I don't know if the difference is only in the headers, or if the shared libraries (dll-s) are also different. If this is the case, does this mean that I have to distribute my application with qt libs enabled, so if someone has them for another compiler, it will not be confused? (Good memory and code sharing, right?). Or are they the same, and in any case, is there some kind of unwritten law?

EDIT2:

I installed another version of qt (msvc 2010) to see what is and is not used. It seems that the shared libraries (they are really separated then) are different. It seems that I really should provide qt-libs to my application then ... And this is not at all what I need (e.g. QtGui 8-9MB). What about other, smaller libraries whose authors are not so kind to provide versions for other compilers? Does this mean that I'm stuck with their original compiler? What if I want to use two different libraries that were compiled by different compilers?

+4
source share
2 answers

Basically you ask about ABI.

In some cases (for example, Itanium) there is a document that is indicated by the ABI, and essentially everyone follows the document.

In the case of DirectX, this is a little the same: Microsoft has published specifications for COM, so anyone who follows these specifications can interact with almost any COM object (within reason - the 64-bit compiler is probably not working with 16-bit COM object written for Windows 3.1).

For most other things, you are more or less on your own. Often, at least, a little is published in the documentation, but, at least in my experience, it often looks at some details that are ultimately important, is not updated completely reliably, and in some cases is simply incorrect. In most cases, it is also poorly organized, so you can very well understand what you can, wherever you find it, and when you run out of information (or patience), do some reverse engineering to fill in the missing parts.

Edit: for non-commercial things like Qt libraries, you have several options. One of them is linking to Qt libraries statically, so the Qt code you need is directly linked to your executable. If you want to use a DLL, then yes, you are pretty much stuck in distributing one with your application, which will be specific to the compiler that you use to create the application itself. Another compiler (or even another version or set of compilation flags with the same compiler) usually requires a different DLL (possibly the same source code, but built-in to change the compiler).

There are exceptions to this, but they are pretty much similar to those described above. For example, the Intel compiler for Windows typically uses the Microsoft standard library and can use most (if not all) of the other libraries created for the Microsoft compiler. This is largely due to Intel leaning back to ensure that their compiler uses the same calling convention, name change scheme, etc., like Microsoft. This works because they put a lot of effort into getting it to work, and not because of any unwritten laws or something like that.

+8
source

DirectX is a COM-based technology and therefore can be used in C with various compilers. Under the hood, the COM interface is a C-like structure that emulates VMT.

Technically, DirectX has a mid-autogenerated C ++ interface, but the general assertion that you can use classes exported to DLL files for different compilers is incorrect.

Edit1:

QT DLLs built using MSVC will not be compatible with those created by gcc, unfortunately, due to natural reasons: different C ++ compilers - different ABIs and different runtime libraries (mingw uses the older MSVCRT, and it therefore, pure -C. DLLs can be used in MSVC and vice versa). Some compilers accidentally or partially intentionally match their ABIs, but this definitely does not apply to MSVC / gcc. QT can also be built as a static library, so you can simply link statically to redistribute things.

The definition of names for C ++ classes in a DLL depends mainly on the compiler interface used. Many commercial compilers from well-known companies use the EDG C ++ parser, so class names overloaded functions have similar or comparable signatures.

Edit2:

"What if I want to use two different libraries that were compiled by different compilers?"

If you desperately need certain functionality from both libraries (I mean some specific operation, not a general structure), then a way that does not have the library source code is to write some shell and compile this shell in C- style.dll.

Think of it as "two different C ++ - es, C ++ - 1 and C ++ - 2". The problem with ABI / Runtime is no different than using Pascal C code or linking to some older Fortran files.

+2
source

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


All Articles