views:

51

answers:

3

Hi, I have build a dll and now I want to use this dll in a Microsoft Visual Studio project.

g++ -O0 -Wall -c -fmessage-length=0 -osrc\MyLib.o ..\src\MyLib.cpp
g++ -shared -Wl,--out-implib=MyLib.lib -Wl,--output-def=MyLib.def -oMyLib.dll src\MyLib.o -lwsock32

The dll works fine when I use it in a "gcc project".

I have tried different methodes to create the ".lib" and ".def" files and tried to import these libs in VS by following different tutorials. But VS does not find the methodes declared in the dll...

I'm thankfull for any help.

+2  A: 

Have you heard about name mangling? Unless the functions exported from DLL are marked as extern "C" their names are going to be mangled in a compiler-specific way. Thus the problem.

Armen Tsirunyan
And to make it worse, it is not only about the name mangling, also other stuff like exception handling or object memory layout is incompatible. And this is not limited to different compilers , also different versions of one compiler, or compiler invocations with different options can be incompatible to each other.
Rudi
@Rudi: +1. Not to mention different implementations of the Standard Library.
Nemanja Trifunovic
A: 

If you want your dll to be used with another compiler you have the following options:

  1. Expose only "C" interface (with extern "C") - no classes or anyting C++ specific.
  2. Make a COM dll.
  3. Make your on COM-like model that follows the same constraints.
Nemanja Trifunovic
I think pure abstract classes are also compiler-independent (to some extent at least).
Pedro d'Aquino
@Pedro: That's what I meant by the option 3) although it is a little more complicated than just using abstract classes.
Nemanja Trifunovic
GCC and MSVC can both be used to make COM dlls - which is a binary interface based on having compliant implementations of virtual functions. IT should be possible to pass pure virtual classes between MSVC and GCC safely (Again, assuming the parameter types are POD or pure virtual classes)
Chris Becke
How can I create a COM dll (Binary interface / ABI) dll using gcc? I think I use the wrong keyword because i find nothing to this topic.
Sascha
@Sascha: Start from here to see how it works (although the samples use pure C it is easy to convert it to C++) http://www.codeproject.com/KB/COM/com_in_c1.aspx
Nemanja Trifunovic
A: 

Thanks for the answers. I cannot think much more so I will try "extern "C"" tomorrow.

Sascha