views:

265

answers:

2

Hello,

I would like to ask in more detail about a answer I recently got here (3rd one): Compiled languages basics

If I write in C and MinGW and I link to C++ library compiled by VC - will it work? How do I know in advance?

In other words, if I'm able to create without warnings an .exe which links to that C++ .dll, and I'm able to run (just run, no further testing) that .exe, does it mean it worked? Wont it core-dump at some point?

To be totally sure, do I need to re-compile the library sources by myself and the link with it?

I understand that there might be a problem sometimes with linking C++ and C code but how to know when it works and worked?

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

+2  A: 

Yes, you can convert MSVC compiled libraries for MinGW without recompiling.
You need only couple of tools and the dll. Check out here.

Regarding mixing C and C++ - if you are not worried about the size of your binary, just use c++ compiler for your c projects. This way you will be not worried about the possible issues with the libs you are linking to.

Andrejs Cainikovs
+1 for linking to the tools, but assuming that C will compile as C++ with no errors is very naive.
Pete Kirkham
+1  A: 

If I write in C and MinGW and I link to 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 might crash if it does link:

  1. The C++ library is exporting C++ classes and methods, using "mangled" names, but MinGW's C++ name mangling may (I don't know) be different than VC's (and non-existent when you're coding in C instead of C++)

  2. VC code doesn't use the same C run-time library as MinGW, which will bite you if the API is such that memory is allocated on the heap by VC code and is then supposed to be freed by the MinGW code.

  3. VC's code isn't binary-comptible with MinGW's code (doesn't use the same parameter-passing conventions, doesn't implement exceptions in the same way)

On the other hand, some reasons why it might work:

  1. The C++ library is written with C-style interface, by developers who intended it to be called from a different compiler

  2. Same as 1.

  3. The makers of the MinGW compiler made it binary-compatible with VC

How do I know in advance?

I don't know. If you post the names of the functions exported from the DLL, and/or the header file which declares its public/exported API, that would give me (or someone else) a very strong hint about whether it's exporting (possibly-incompatible) C++-style methods or exporting (more-likely-to-be-comptible) C-style functions.

Otherwise you have a two-stage question:

  1. What does it take (e.g. C instead of C++, e.g. not assuming they use the same heap, e.g. specifiying the parameter-passing convention) for MinGW code to invoke VC code?

  2. Has your VC library be written with that in mind?

Someone who has used both compilers could probably answer the first question (I haven't used MinGW). I don't know who could answer the second; who wrote that VC library?

ChrisW
"who wrote that VC library?"It was just an example but say python26.dll...And if I'm able to run the resulting .exe does it mean it worked 100%?
Anton L.
I could say whether it looks like it's designed to be cross-compiler-safe API, if I could see its header file (or even the list of names of the functions it exports). Also your being able to run it doesn't guarantee 100% (e.g. there may be heap corruption which won't show up until later, or e.g. it's OK until an exception is throw) ... but, being able to run it is certainly seems like a good step in the right direction!
ChrisW