Can a static libary *.a
in Linux be dynamically loaded at runtime?
I've read here that
...both static and shared libraries can be used as dynamically loaded libraries.
How to dynamically load static library?
Can a static libary *.a
in Linux be dynamically loaded at runtime?
I've read here that
...both static and shared libraries can be used as dynamically loaded libraries.
How to dynamically load static library?
A static library is more or less just a collection of object files. If you want to use a static library in a program, you have to link the executable with it. The executable will then contain the static library (or the parts that you used).
If you want to load a static library at runtime using dlopen
, you will have to first create a dynamic library libfoo.so
containing it.
Opening a .a
file using dlopen
does not work (tested on Ubuntu 10.04). With the following example program:
#include <dlfcn.h>
#include <stdio.h>
int main()
{
void *lib_handle = dlopen("/usr/lib/libz.a",RTLD_LAZY);
printf("dlopen error=%s\n",dlerror());
printf("lib_handle=%p\n",lib_handle);
}
I get:
dlopen error=/usr/lib/libz.a: invalid ELF header
lib_handle=(nil)
while when using /usr/lib/libz.so
instead, I get:
dlopen error=(null)
lib_handle=0x19d6030
so the same code works for a shared object.