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 handle
as the NULL
pointer:
void *handle;
char *error;
handle = dlopen ("./hw11-lib-michaelSchilling.so", RTLD_LAZY);
//same error comes up with full path as well as './hw11...'
if(!handle){
error = dlerror();
printf("%s\n", error);
printf("Error loading library.\n");
exit(1);
}
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
only ET_DYN and ET_EXEC can be loaded.
Library references are more efficient because the library procedures are statically linked into the program. Static linking increases the file size of your program, and it may increase the code size in memory if other applications, or other copies of your application, are running on the system.
Dynamic linking from Assembler language programs in IBM OS/360 and its successors is done typically using a LINK macro instruction containing a Supervisor Call instruction that activates the operating system routines that makes the library module to be linked available to the program.
Programs that are linked dynamically are linked against shared objects that have the extension . so. An example of such an object is the shared object version of the standard C library, libc.so.
Dynamic libraries are linked during the execution of the final executable. Only the name of the dynamic library is placed in the final executable. The actual linking happens during runtime, when both executable and library are placed in the main memory.
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
A slash (/
) as the first character of a pathname indicates that the pathname is absolute (relative to the root directory), not relative to the current working directory and certainly not relative to the location of the binary. You'll need to specify the full path by figuring out the location of the binary (which is not an easy problem in itself) or you might be able to use $ORIGIN
with dlopen
(it works with rpath but I'm uncertain whether it works with dlopen
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With