Here is a test case:
I compile the following into a DLL using gcc:
------------csubs.c----------------
#include <stdio.h>
void csub(void) {
puts("In c sub");
}
---------------------------------------
gcc csubs.o -shared -o csubs.dll \
-Wl,--out-implib=csubs.lib \
-Wl,--export-all-symbols \
-Wl,--enable-auto-import
Then I create a Visual Studio project in C++ and create the following
test program:
----------------------------------------
#include <iostream>
using namespace std;
extern "C" {
void csub(void);
}
int main(void) {
puts("Starting program");
csub();
puts("Finished program");
cin.get();
}
---------------------------------------
In order to compile, I drag and drop the import library csubs.lib into
the project. Like I mentioned above, this does work when I use
"Debug" mode in Visual Studio. In "Release" mode, it still compiles
and links, but I get a segfault when running. I've tried fooling
around with some of the compiler options in Visual Studio to see if I
could figure out which particular "Release" option is causing the
problem, but no luck. I suspect something is not compatible between
the way the DLL was created by gcc and the way Visual Studio is
accessing it, but I don't know enough about Windows/Visual Studio/
DLL's to figure it out.
Any help is much appreciated.
John
I figured out what is causing the problem and how to fix it, although
I'm still not sure why it is a problem. I was able to get the program
to run in Release mode by turning off "Eliminate Unreferenced Data"
from the linker optimization options. Apparently the DLL was being
completely removed as a dependency (this makes sense because I noticed
that it wasn't even complaining if I didn't have the DLL in the path).
I would still be interested if someone could explain why this was
happening in the first place. Maybe I didn't create the *.lib file
correctly?
John
lib files produced by gcc are not compatible with MSVC:
http://www.mingw.org/wiki/MSVC_and_MinGW_DLLs
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=12987
Short: I have used the lib.exe program shipped with MSVC to produce an
MSVC-compatible lib file from a .def file I had got generated by gcc
for the dll.
Something like:
c:\devstudio\vc98\bin>lib /out:mydll.lib /def:mydll.def /machine:ix86
The resulting mydll.lib is then compatible with MSVC linker.
And don't forget to turn "Eliminate Unreferenced Data" back on, since
it actually does good thing and is definitely not the reason for the
problem you have.
Note: Debug mode runs fine, because it by default turns "Eliminate
Unreferenced Data" off.