views:

183

answers:

5

I am trying to build a dll using mingw g++ under cygwin.

I have .h files that refer to objects exported by a .dll that was build using MS Visual Studio 2008. I link to the .lib generated with that .dll.

When I run g++ I get lots of errors like this

/cygdrive/c/dev/blox/ulfx/ulfx/JavaInterface.cpp:206: undefined reference to `__imp___ZNK18DLLSafeDoubleArray4sizeEv'

The .dll that I want to link to exposes a function named

?size@DLLSafeDoubleArray@@QBEIXZ

And I assume that the problem here is that the g++ name mangling is incompatible with the VS2008 name mangling.

Is there a way I can force g++ to use a compatible mangling ?

I do not have any version of Visual Studio installed on my machine.

+1  A: 

G'day,

Not sure if this is going to be possible as there is too much that can vary between compilers from different vendors apart from just the name mangling. Things such as:

  1. this handling,
  2. layout and contents of v-tables,
  3. class layout,
  4. parameter sequence,
  5. register handling,
  6. return value handling,
  7. etc.

I'd be interested to see if it was possible though.

Rob Wells
http://en.wikipedia.org/wiki/Name_mangling#Real-world_effects_of_C.2B.2B_name_manglingdoesn't fill me with alot of optimism.
Ben Hammond
@Ben, Yikes! It's not looking good. Sorry mate.
Rob Wells
+1  A: 

Maybe this page helps:

http://www.mingw.org/wiki/MSVC%5Fand%5FMinGW%5FDLLs

I did manage to do something once but it was just a prototype which I abandoned when I found a library doing what I wanted. I did follow their instructions though, but I was producing the dll myself.

laura
nice link.I have created a .def file from the .dlland created a .a file from the .defand removed the old .lib to ensure that the .a file is being used.But it hasn't made any difference.Hmm.
Ben Hammond
Yes I distinctly remember this being a pain to get working - I did manage eventually, but it took lots of tinkering in the VSExpress code. I hope you succeed though.
laura
+1  A: 

I agree with Rob Wells comment, there are a lot of things in C++ that may break.

Something which should be easier, safer, and possible would be to use plain C functions, as the main thing to worry about there is the calling convention and potentially packing and alignment of structs.

dalle
I totally agree with you.Unfortunately I am unable to modify the dll.I guess my only solution is to get a copy of VS2008.
Ben Hammond
There is VS2008 Express which is free. It might help you. :)
Marcus Lindblom
Also +1 for wrapping with C. The C ABI works regardless of compiler.
Marcus Lindblom
A: 

Better way is to recompile dll with g++. If it is possible.

denisenkom
A: 

Object code produced by different C++ compilers is not link compatible.

This is done on purpose: compilers use different ABI, and if the name maingling didn't prevent you from linking, you'd now be wondering/debugging why the simplest operations are causing your executable to crash.

You should be grateful that the different name mangling scheme spared you that debugging effort.

Employed Russian
But I'm ultimately trying to reference a dll.So I can (in theory) use the GNU dlltool to generate a compatible .a file from the dll, and then link to that, as laura answered above ?(I still haven't managed to get that bit working though).
Ben Hammond
No, you can't: it's not a matter of making "import A" and "export B" to match. The problem is that "export B" provides "square pegs" which do not fit into "round holes" required by "import A" -- the actual bytes at "*this" are *different*, and there is nothing an external tool can do about this.
Employed Russian