views:

63

answers:

2

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?

+1  A: 

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.

JesperE
That workaround implies that I would have to create a shared library from a static library if I want to dynamically load it.This means that loading a static library dynamically is not possible and that only shared libraries can be used for dynamical loading?If so, than the quote I've stated from the source is not correct.
kobac
The quoted text is incorrect, or I am misinterpreting what they mean.
JesperE
A: 

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.

Andre Holzner