Reference libraries compiled by various compilers

I would like to ask in more detail the answer I recently received (third): Compiled Language Basics

If I write in C and MinGW and I link to a C ++ library built by VC, will it work? How will I know in advance?

In other words, if I can create, without warnings, an .exe that references this C ++. Dll, and I can run (just run, without further testing) that .exe, does that work? Wouldn't it be a core dump at some point?

To be absolutely sure I need to recompile the library sources myself and link with it?

I understand that sometimes there can be a problem with referencing C ++ and C code, but how do you know when it is up and running?

PS: Yes, I saw Use compiled libraries ... I just thought my question was slightly different.

+1


source to share


2 answers


If I write in C and MinGW and I link to a C ++ library compiled by VC - will it work?

It depends on the compilers (and I don't know MinGW) and on the specific C ++ library.

Reasons why it might not link, or it might crash if it links:

  • The C ++ library exports C ++ classes and methods using "mangled" names , but MinGW C ++ name mangling may (I don't know) differ from VC (and doesn't exist when you code in C instead of C ++)

  • The VC code does not use the same C runtime library as MinGW, which will bite you if the API is such that memory will be allocated on the heap by VC code and then must be freed by MinGW.

  • VC code is not binary compatible with MinGW code (does not use the same parameter passing conventions, does not implement exceptions in the same way)

On the other hand, there are some reasons why this might work:

  • The C ++ library is written with a C-style interface, developers who assumed it would be called from another compiler

  • Same as 1.

  • MinGW compiler made it binary compatible with VC



How will I know in advance?

I dont know. If you post the names of the functions exported from the DLL and / or the header file that declares its public / exported API, that would give me (or anyone else) a very strong hint as to whether it exports (possibly incompatible) C ++ -style methods or export (more likely to be possible) C-style functions.

Otherwise, you have a two-step question:

  • What is required (for example C instead of C ++, for example without assuming they are using the same heap, such as the specification of the parameter passing convention) for the MinGW code to invoke the VC code?

  • Will your VC library be written with this in mind?

Someone who has used both compilers might be able to answer the first question (I have not used MinGW). I don't know who could answer the second; who wrote this VC library?

+1


source


Yes, you can convert compiled MSVC libraries for MinGW without recompiling.
You only need a few tools and dlls. Browse here .

As far as mixing C and C ++ - if you are not worried about the size of your binary, just use the C ++ compiler for your c projects. This way you won't be worried about potential problems with the libs you link to.



+5


source







All Articles