Loading gnu made DLL with MSVC

Massimo CICCOTELLI 0994cicc@s1.cise.it
Tue Sep 8 09:01:00 GMT 1998


Hi all!!

We are looking with great interest at the debate on how to make
DLL with gcc which can be loaded by MSVC.

Picking up from different mails on the argument and after some 
trials, we managed to build up a simple DLL (with egcs-1.0.2-
mingw32 by Mumit) which can correctly loaded in MSVC 5.0. 
Here is how:

- buid test.dll and test.exe which uses it using the following 
makefile

---------------------------------- makefile -----------------------------------------------
default: test.exe
#
test.exe: test.o libtest.a
	gcc test.o -o test.exe -L./ -ltest
#
import: libtest.a

libtest.a: test.dll test-exp.def
	dlltool --dllname test.dll --def test-exp.def --output-lib libtest.a
#
export: test.dll

test.dll: testlib.o test-exp.def
	gcc -mdll -o junk.tmp -Wl,--base-file,test.tmp testlib.o
	rm junk.tmp
	dlltool --dllname test.dll --base-file test.tmp --output-exp 
test.exp --def test-exp.def
	rm test.tmp
	gcc -mdll -o test.dll testlib.o -Wl,test.exp
	rm test.exp

testlib.o: testlib.c
	gcc -c testlib.c

test.o: test.c
	gcc -c test.c
---------------------------------- End of makefile --------------------------------------

where test.c testlib.c and test-exp.def are:

---------------------------------- test.c -----------------------------------------------
#include <stdio.h>

int main(){

   printf(" 2+7=%d\n",intsum(2,7));
   printf(" 5+8=%d\n",intsum(5,8));
   printf(" 1+4=%d\n",intsum(1,4));

   return 0;
}
------------------------------- end of test.c --------------------------------------

------------------------------- testlib.c ----------------------------------------
int intsum(int arg1, int arg2) { 
   return arg1+arg2;
}
------------------------------ end of testlib.c ------------------------------

----------------------------- test-exp.def ----------------------------------
EXPORTS
   intsum
--------------------------- end of test-exp.def --------------------------

Now you can run test.exe. Its results should be obvious.
Moreover, you can create an import library (.LIB) with MSVC
with the following command:

lib /DEF:test.def

where test.def is the following file:

--------------------------- test.def -----------------------------------
LIBRARY test

EXPORTS
   intsum
------------------------- end of test.def ---------------------------

obtaining test.lib. Finally you can create a new test.exe with MSVC
with the following command:

cl test.c /link test.lib

Another way of using test.dll is by loading it directly from the
source code, that is, by means of  the following code:

---------------------------- loadlib.c ----------------------------------------
#include <stdio.h>
#include <windows.h>

int main()
{

HINSTANCE dllHandle=NULL;
typedef int (*INTSUM) (int,int);
INTSUM intsum;

dllHandle=LoadLibrary("test.dll");
intsum=(INTSUM)GetProcAddress(dllHandle,"intsum");

   printf(" 2+7=%d\n",intsum(2,7));
   printf(" 5+8=%d\n",intsum(5,8));
   printf(" 1+4=%d\n",intsum(1,4));

   return 0;
}
-------------------------- end of loadlib.c ----------------------------------

You can create loadtest.exe with gcc by means of the following
makefile:
--------------------------- makeload ------------------------------------
#
default: loadlib.exe
#
loadlib.exe: loadlib.o
	gcc loadlib.o -o loadlib.exe
#
loadlib.o: loadlib.c
	gcc -c loadlib.c
-------------------------- end of makeload ---------------------------

or with MSVC with the following command:

cl loadlib.c

Hope this helps!
                                                 Ciao, Massimo & Lorenzo
-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request@cygnus.com" with one line of text: "help".



More information about the Cygwin mailing list