Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Getting dynamic loading to work on cygwin



In case anyone has a chance to play around with it, here's a minimal
example of getting dynamically loaded libraries to work on cygwin.  The
changes to what we have already should be fairly mechanical but will still
require a bit of work.  Zefram's and Oliver's work on export files will
come in handy, since it looks like cygwin will need a very similar set to
AIX (there are tools to generate them automatically, but we have enough
in place already to get it right).  Just unpacking these and running make
should create a main.exe and a dl.dll; running the first loads the second.
I actually found out about the dllwrap incantation (which does all the hard
work for you) from the perl dynamic loading configuration.

There may be some issues about search paths still to resolve, since there
are both .a (list of exports) and a .dll (the actual library) files; I
haven't tried moving the files into different directories.  It might also
be more efficient to use dlltool directly rather than rely on dllwrap, but
I for one don't have the patience.

In case anyone is any doubt, this will *not* be in the release which is
immediately due.

.SUFFIXES: .so .dll

CC = gcc
CFLAGS = -O -g -Wall
LIBS = # -ldl

all: main dl.dll

main: main.o
	$(CC) -o main main.o $(LIBS)

dl.o: dl.c
	gcc $(CFLAGS) -c dl.c

dl.dll : dl.o
	dllwrap --dllname $*.dll --driver-name gcc --dlltool dlltool --as as --def $*.def --output-lib lib$*.a dl.o

dl.so : dl.o
	gcc -G -o dl.so dl.o

clean:
	rm -f dl.o dl.so dl.dll main.o main libdl.a main.exe dl.base
#include <stdio.h>
#include <dlfcn.h>

int main(int argc, char **argv)
{
    void *handle, *symbol;
    int ret;

    handle = dlopen("./dl.dll", RTLD_LAZY);
    if (handle == NULL) {
	fprintf(stderr, "dlopen failed.\n");
	return 1;
    }
    symbol = dlsym(handle, "module");
    if (symbol == NULL) {
	fprintf(stderr, "dlsym failed.\n");
	return 1;
    }
    ret = ((int (*)(void))symbol)();

    printf("module returned %d\n", ret);

    return 0;
}
#include <stdio.h>

int module(void)
{
    printf("This is the module.\n");
    return 42;
}

EXPORTS
	module


Messages sorted by: Reverse Date, Date, Thread, Author