exporting symbols from executable and linking shared objects question
Mariusz Woloszyn
cygwin@clubbing.pl
Wed May 23 06:51:00 GMT 2001
Hi!!
I'm new to the list, but I searched archives looking for a solution and
found nothing.
I have TWO questions, but it's the same problem.
First is -rdynamic swich for gcc. it's used to export symbols for shared
objects and the second is about linking shared objects (.dll) with symbols
declared as external.
Let's see following code:
---main.c---
#include <dlfcn.h>
#include <stdio.h>
int c;
main () {
int (*foo)();
void *handler = dlopen ("./foo.so", RTLD_LAZY|RTLD_GLOBAL);
if (!handler) {
fputs (dlerror(), stderr);
printf("\n");
exit(1);
}
foo = dlsym(handler,"foo");
(*foo)();
dlclose(handler);
}
---main.c---
and following shared object source:
---foo.c---
//extern int c;
foo () {
printf("Fooo!\n");
// printf("c=%i\n",c);
}
---foo.c---
If I compile it under Unix or windows (under Win using attached shell
script -- refering to the documentation) everything is OK. But WHAT I have
to do to get it working with foo looking like this:
---foo.c---
extern int c;
foo () {
printf("Fooo!\n");
printf("c=%i\n",c);
}
---foo.c---
Under Unix I used to compile main:
gcc main.c -o main -ldl -rdynamic
(to export symbols to dynamically dlopened shared objects), and foo:
gcc -shared foo.c -o foo.so
First of all -rdynamic does not work under cygwin and moreover ld fails
linking shared objects with symbols declared as external.
Any suggestions???
Regards,
--
Mariusz Wołoszyn
Internet Security Specialist, Internet Partners
-------------- next part --------------
#! /bin/sh
# Example Script to compile and link a relocatable DLL
# Files that make up the DLL = $foo.c init.cc fixup.c.
# (init.cc and fixup.c are housekeeping routines needed for the DLL. The actual
# library routines are in $foo.c and )
# ***Fill in your path to libcygwin.a here (with no trailing slash)***
LIBPATH=/lib
echo "Compiling" `basename $1 .c`".dll"
foo="`basename $1 .c`"
echo $foo
# Compile source files:
gcc -c $foo.c
gcc -c init.cc
gcc -c fixup.c
# Make .def file:
echo EXPORTS > $foo.def
nm $foo.o init.o fixup.o | grep '^........ [T] _' | sed 's/[^_]*_//' >> $foo.def
# Link DLL.
ld --base-file $foo.base --dll -o $foo.dll $foo.o init.o fixup.o \
$LIBPATH/libcygwin.a /lib/w32api/libkernel32.a -e _dll_entry@12
dlltool --as=as --dllname $foo.dll --def $foo.def --base-file $foo.base --output-exp $foo.exp
ld --base-file $foo.base $foo.exp --dll -o $foo.dll $foo.o init.o fixup.o \
$LIBPATH/libcygwin.a /lib/w32api/libkernel32.a -e _dll_entry@12
dlltool --as=as --dllname $foo.dll --def $foo.def --base-file $foo.base --output-exp $foo.exp
ld $foo.exp --dll -o $foo.dll $foo.o init.o fixup.o \
$LIBPATH/libcygwin.a /lib/w32api/libkernel32.a -e _dll_entry@12
# Build the $foo.a lib to link to:
dlltool --as=as --dllname $foo.dll --def $foo.def --output-lib $foo.a
# Linking with main
#gcc main.c $foo.a -o main.exe
More information about the Cygwin
mailing list