This is the mail archive of the cygwin@sourceware.cygnus.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

RE: Can't build a DLL using gcc


You know when I first used cygwin to build dll's I tried using that default cygwin
FAQ you lised below and had similar problems. I think the problem with that thing is
that it is outdated as hell. Mummit Khan has samples on his web page of makefiles
that will build a good DLL with little work on your part. Here is the one I have used:

#
# Makefile for Cygwin in -mno-cygwin mode. This builds a DLL that is
# independent of Cygwin DLL and only depends on MS CRTDLL runtime.
#
# If you want to use Mingw32 gcc, then use the makeit.bat file instead.
#
CC = gcc

# The root directory for mingw-extra package. You can get this package
# from the same place where you downloaded egcs-1.1.2-cygb20 from.
# You should also read the -mno-cygwin howto available from:
#  http://www.xraylith.wisc.edu/~khan/software/gnu-win32/
#
MINGW_EXTRA_ROOT = /usr/local/mingw-extra
MINGW_INCLUDES = -I$(MINGW_EXTRA_ROOT)/include
MINGW_LIBDIRS = -I$(MINGW_EXTRA_ROOT)/lib

DEBUG = -g -O2
# The -mrtd flag tells the compiler to use the PASCAL or "stdcall" calling
# convention required by VBA/Excel/etc.
CFLAGS = -mrtd $(DEBUG)
CPPFLAGS = $(MINGW_INCLUDES)

AS = as
DLLTOOL = dlltool
DLLWRAP = dllwrap

#
# Various targets to build.
#
DLL_NAME = test.dll
DLL_EXP_DEF = test.def

all: $(DLL_NAME)

#
# sources, objects, etc. 
#
SRCS  = $(wildcard *.c)
OBJS := $(OBJS:.c=.o)

#
# DLL related variables. These are used when building the DLL. See later.
#

# any special flags that you need to pass to the F77 compiler when building 
# the DLL. (lots of software need _DLL defined).
DLL_CFLAGS = -DBUILDING_DLL=1
# This is where you pass linker flags, such as library search path, alternate
# entry point, etc. The -s flag strips the final DLL.
DLL_LDFLAGS = $(MINGW_LIBDIRS) -s -W1,-e,_cygwin_nocygwin_init@12
# any extra libraries that your DLL may depend on.
DLL_LDLIBS = 

#
# your sources etc that go into the DLL.
#
DLL_SRCS = test.c 
DLL_OBJS = test.o 

###
#
# Making DLL
#
###

#
# Note that we let dllwrap create both the DEF and IMPORT library in
# one shot. No need to run dlltool anymore.
#
DLLWRAP_FLAGS = --export-all --output-def $(DLL_EXP_DEF) \
	--target=i386-mingw32 -mno-cygwin \
	--driver-name $(CC)

$(DLL_NAME) $(DLL_EXP_DEF): $(DLL_OBJS)
	$(DLLWRAP) $(DLLWRAP_FLAGS) -o $(DLL_NAME) \
	    $(DLL_OBJS) $(DLL_LDFLAGS) $(DLL_LDLIBS)

#
# dependencies.
#

#fdll.o: fdll.c

test.o: test.c


#
# default rules for building DLL objects. Note that client programs (ie.,
# the ones that *use* the DLL) have to be compiled without the DLL_CFLAGS
# flags.
#
.c.o:
	$(CC) -c $(CPPFLAGS) $(DLL_CFLAGS) $(CFLAGS) -o $@ $<

clean:
	-rm -f $(OBJS) $(DLL_OBJS) $(DLL_NAME) $(DLL_EXP_DEF) 


Hope this helps, it sure helped me. 

Eric

-----Original Message-----
From:	kulack@us.ibm.com [SMTP:kulack@us.ibm.com]
Sent:	Thursday, June 15, 2000 5:12 PM
To:	cygwin@sourceware.cygnus.com
Subject:	Can't build a DLL using gcc

I'm pretty new at Win and gcc development. I've been searching the archives
for details about this, but have only found hints.
I've followed the instrutions at
http://sourceware.cygnus.com/cygwin/cygwin-ug-net/dll.html for building
DLLs using GCC, but get a STATUS_ACCESS_VIOLATION when I attempt to use it.

About my only guess is that I'm doing something wrong with the .DEF file.
Can it get created automatically or should there be a 'SECTIONS' section in
the .DEF file indicating which type of access (EXECUTE, READ, WRITE) the
dll should have?
Not familiar with nm output, but it seems to look ok for the created dll...
Can anyone teach me to fish? 8-)

Bash /db2src/test
 > make libhello.a
gcc  -g -c -I/sqllib/include libhello.c
gcc -s -Wl,--base-file,libhello.base -o libhello.dll libhello.o
-Wl,-e,_mydll_init@12
dlltool --base-file libhello.base --def libhello.def --output-exp
libhello.exp --dllname libhello.dll
gcc -s -Wl,--base-file,libhello.base,libhello.exp -o libhello.dll
libhello.o -Wl,-e,_mydll_init@12
dlltool --base-file libhello.base --def libhello.def --output-exp
libhello.exp --dllname libhello.dll
gcc -Wl,libhello.exp -o libhello.dll libhello.o -Wl,-e,_mydll_init@12
dlltool --def libhello.def --dllname libhello.dll --output-lib libhello.a
cp libhello.dll ..
rm libhello.o

Bash /db2src/test
 > make hello.exe
gcc  -g -c -I/sqllib/include hello.c
gcc  -g hello.o -L/db2src/test -lhello
mv a.exe hello.exe

Bash /db2src/test
 > hello
Hello World
      0 [main] hello 1127 handle_exceptions: Exception:
STATUS_ACCESS_VIOLATION
   9205 [main] hello 1127 stackdump: Dumping stack trace to
hello.exe.stackdump

Bash /db2src/test
 > cat hello.exe.stackdump
Exception: STATUS_ACCESS_VIOLATION at eip=000055CC
eax=00000009 ebx=00000000 ecx=0000000C edx=0245FDF4 esi=00000000
edi=0A041448
ebp=0245FE74 esp=0245FE58 program=c:\db2src\test\hello.exe
cs=001B ds=0023 es=0023 fs=003B gs=0000 ss=0023
Stack trace:
Frame     Function  Args
0245FE74  00401085  (00000009, 0245FEB0, 0245FEA4, 0040106B)
0245FEA4  00401085  (00000001, 0A041448, 0A040008, 00000000)
0245FF00  610022E5  (7FFDF000, 00000000, 00000018, 80452662)
0245FF60  61002735  (00403010, 00000000, 0245FF90, 004011DA)
0245FF90  004011E7  (00401060, FFFFFFFF, 80430B27, 00000000)
0245FFC0  0040103B  (00000018, 00000000, 7FFDF000, 00000000)
0245FFF0  77E87903  (00401000, 00000000, 000000C8, 00000100)
End of stack trace

======= libhello.c
#include <stdio.h>
#include <windows.h>
extern int helloFromDll(int parm);

int WINAPI
mydll_init(HANDLE h, DWORD reason, void *foo)
{
   return 1;
}

int helloFromDll(int parm) {
   printf("Hello from Dll, parm=%d\n", parm);
   return parm+1;
}

int main(int argc, char **argv) {
   // This doesn't feel right...
   // Was forced to add main() in order to get the
   // instructions for building DLLs at
   //   http://sourceware.cygnus.com/cygwin/cygwin-ug-net/dll.html
   // to work correctly, otherwise, linker failed to
   // find WinMain@16 (which I wouldn't have believed it should
   // require).
}

======= libhello.def
LIBRARY LIBHELLO
DESCRIPTION 'LIBHELLO.DLL Testcase'
EXPORTS
   helloFromDll

======= hello.c
#include <stdio.h>
extern int helloFromDll(int parm);
int main(int argc, char **argv)
{
   int         rc;
   printf("Hello World\n");
   rc = helloFromDll(9);
   printf("DLL returned %d\n", rc);
   return 0;
}




--
Want to unsubscribe from this list?
Send a message to cygwin-unsubscribe@sourceware.cygnus.com


--
Want to unsubscribe from this list?
Send a message to cygwin-unsubscribe@sourceware.cygnus.com


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]