shared libraray
Kevin Su
ksu@kinaare.net
Fri Nov 24 09:22:00 GMT 2000
Hi,
I recently tried to create a shared library. I found that if I
compile the source with g++, instead of gcc, symbol of functions in the
shared library can not be found with dlsym(), it returns NULL with an
error from dlerror(). With gcc, everything is working fine.
To test it, I wrote two sample file, xxx.c and test.c, shown below.
Compiling these two files using gcc like the following will work:
$ gcc -fpic -c xxx.c
$ ld -shared xxx.o -o xxx.so
$ gcc test.c -ldl
$ a.out
xxxxxxxx
Compiling these two files using g++ like the following will NOT work:
$ g++ -fpic -c xxx.c
$ ld -shared xxx.o -o xxx.so
$ g++ test.c -ldl
$ a.out
dlsym:/tmp/xxx.so: undefined symbol: xxx
Any help or hint on this problem is much appreciated.
Thanks,
Kevin
file "xxx.c" ( the file to be built into a shared library):
====================================================
#include <stdio.h>
void
xxx(void)
{
fprintf(stderr, "xxxxxxxx\n");
}
file "test.c" (the file that loads the shared library) :
=====================================================
#include <dlfcn.h>
#include <stdio.h>
typedef void (*func)(void);
int main()
{
void* ptr;
func f;
void* handle;
if((handle = dlopen("/tmp/xxx.so", RTLD_LAZY)) == 0){
fprintf(stderr, "dlopen:%s\n", dlerror());
return 0;
if((ptr = dlsym(handle, "xxx")) == 0){
fprintf(stderr,"dlsym%s\n", dlerror());
return 0;
}
f = (func)ptr;
f();
}
More information about the Gcc-help
mailing list