delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/1998/09/09/13:11:37

From: 0994cicc AT s1 DOT cise DOT it (Massimo CICCOTELLI)
Subject: Loading gnu made DLL with MSVC
9 Sep 1998 13:11:37 -0700 :
Message-ID: <9809081517.AA01105.cygnus.gnu-win32@dns.cise.it>
To: gnu-win32 AT cygnus DOT com

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 AT cygnus DOT com" with one line of text: "help".

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019