Subject: Loading gnu made DLL with MSVC
From: [email-masked] (Massimo CICCOTELLI)

9 Sep 1998 09:37:53 -0700
Message-ID: <[email-masked]>
To: [email-masked]

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:

---------------------------- 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 ----------------------------------
-
For help on using this list (especially unsubscribing), send a message to
"[email-masked]" with one line of text: "help".