Michael Schilling Michael Schilling - 3 years ago 91
C Question

Proper use libdl and dynamically linked libraries

I need to dynamically link a library that I have created. I'm not exactly sure what the issue is. It all compiles properly, but I always catch

as the

void *handle;
char *error;
handle = dlopen ("./hw11-lib-michaelSchilling.so", RTLD_LAZY);
//same error comes up with full path as well as './hw11...'
error = dlerror();
printf("%s\n", error);
printf("Error loading library.\n");

I cant get passed this error and I'm not sure what could possibly be wrong. I'm pretty sure I've compiled everything correctly. Here are the compilation steps I used:

gcc -rdynamic -c hw11-lib-michaelSchilling.c -o hw11-lib-michaelSchilling.so
gcc hw11-michaelSchilling-4.c -ldl -o hw11-michaelSchilling-4

I'm getting an error that reads
./hw11-lib-michaelSchilling.so: only ET_DYN and ET_EXEC can be loaded.
What does this mean?


Answer Source

When building hw11-lib-michaelSchilling.so, you don't appear to be telling gcc that you want a shared object (the .so in the name isn't enough).

With the -c it's producing an object file (not a shared object) and calling it michaelSchilling.so. The linker doesn't even get invoked.

Remove the -c from the gcc command line and add -shared:

gcc -shared -rdynamic hw11-lib-michaelSchilling.c -o hw11-lib-michaelSchilling.so
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download