Mixing C / C ++ Libraries

Is it possible for gcc to communicate with a library created using Visual C ++? If so, are there any conflicts / problems that may arise as a result of this?

+4
source share
5 answers

Some of the comments in the answers here are a little too generalized.

Not yet, in the specific case, the mentioned gcc binaries will not be associated with the VC ++ library (AFAIK). The actual means of linking code / libraries is the question of the ABI standard that is used.

An increasingly common standard in the embedded world is the EABI (or ARM ABI) standard (based on work done during Itanium development http://www.codesourcery.com/cxx-abi/ ). If compilers are compatible with EABI, they can create executable files and libraries that will work with each other. An example of several work circuits working together is the ARM RVCT compiler, which creates binaries that will work with GCC ARM ABI binaries.

(The link to the sourcery code is missing at the moment, but can be cached by Google)

+2
source

I would not have guessed. Typically, C ++ compilers have completely different name-changing methods, which means linkers will not be able to find the correct characters. This, by the way, is very good, because C ++ compilers allow the standard to have a much higher level of incompatibility than just this, which will lead to a crash, die, eat puppies and grease paint all over the wall.

Common schemes for working around this usually include language-independent methods such as COM or CORBA. An easier consecrated method is to use C "wrappers" around your C ++ code.

+1
source

It's impossible. Usually you cannot link libraries created by different versions of the same compiler.

+1
source

Yes, if you make a dynamic link and make the c-style interface. lib.exe will generate import libraries that are compatible with the gcc toolchain.

This will solve your binding problems. However, this is only the beginning of the problem.

Your big problems will be like exceptions and memory allocation.

  • You must make sure that the exception did not go from VC ++ to gcc code, there are no guarantees of compatibility.
  • Each object from the VC ++ library will have to live on the heap, because:
  • Do not mix gcc new / delete with anything from VC ++, bad things will happen. This also applies to building an object on the stack. However, if you create an interface such as create_some_obj () / delete_some_obj (), you cannot use gcc new to create VC ++ objects. Perhaps create a small handler object that handles construction and destruction. This way you keep RAII, but still use the c-interface for the true interface.
  • The calling agreement must be correct. In VC ++ there is cdecl and stdcall. If gcc tried to call an imported function with the wrong type of call, bad things will happen.

The bottom line contains a simple ANSI C compatible interface and everything should be fine. The fact that crazy C ++ goes behind is ok if it is contained.

Oh, and make sure all the code is redirected, or you run the risk of opening the whole outline of the worm.

+1
source

No. Normal and simple :-)

0
source

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


All Articles