This is the mail archive of the
cygwin
mailing list for the Cygwin project.
Load a shared library using gcc/Cygwin
- From: "Yu-Cheng Chou" <cycchou at ucdavis dot edu>
- To: cygwin at cygwin dot com
- Date: Tue, 5 Jul 2005 12:11:53 -0700 (PDT)
- Subject: Load a shared library using gcc/Cygwin
Hi,
main.c below is for loading the shared library module.dll
/* command to create main.exe
cl -c main.c
link main.obj
*/
#include <stdio.h>
#include <windows.h>
int main(){
void *handle;
int (*fp)();
char *modname = "./module.dll";
HMODULE h;
void (*init)();
printf("hello1\n");
h = LoadLibrary("cygwin1.dll");
printf("hello1 h = %p\n", h);
init = ( void (*)())GetProcAddress(h, "cygwin_dll_init");
printf("init = %p\n", init);
init(); // CRASH HERE.......!!!
printf("hello2\n");
handle = LoadLibrary(modname);
printf("hello2 handle = %p\n", handle);
if (handle == NULL) {
fprintf(stderr, "Can't load %s in LoadLibrary()\n", modname);
exit(1);
}
fp = ( int (*)())GetProcAddress(handle, "foo");
if (fp== NULL) {
fprintf(stderr, "ERROR: GetProcAddress()\n");
exit(1);
}
printf("ok1\n");
fp();
printf("ok2\n");
FreeLibrary(handle);
}
module.c below is the source code for creating module.dll
/* command to create
gcc -shared -o module.dll module.c
*/
#include <stdio.h>
#include <windows.h>
/*
__declspec(dllexport)
*/
int foo(int arg){
printf("foo() called\n");
return (arg * 2);
}
Makefile is for creating main.exe and module.dll
CC = cl
LINK = link
#CFLAGS = /MD
CFLAGS =
all: main.exe module.dll
main.exe: main.obj
$(LINK) main.obj
main.obj: main.c
$(CC) $(CFLAGS) -c main.c
module.dll: module.o
gcc -shared -o module.dll module.c
#gcc -shared -o module.dll module.o
module.o: module.c
gcc -c module.c
clean:
rm *.obj *.dll *.exe *.lib *.exp *.o main.exe.stackdump
I followed the instructions from FAQ to load a shared library.
But the program main.exe crashed at the line init() highlighted in the
main.c
How can I fix the problem?
Thanks
--
Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple
Problem reports: http://cygwin.com/problems.html
Documentation: http://cygwin.com/docs.html
FAQ: http://cygwin.com/faq/