# A dynamic library that loads other libraries (cannot open file)



## Naos (Aug 31, 2013)

This is my source:

```
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
int main(int argc, char **argv) {
    void *handle;
    double (*cosine)(double);
    char *error;
    //Loadin all my libs
    handle = dlopen ("/test/my_data/my_lib_test.so", RTLD_LAZY);
    if (!handle) {
        fprintf (stderr, "%s\n", dlerror());
        exit(1);
    }
    dlerror();    /* Clear any existing error */
    cosine = dlsym(handle, "cos");
    if ((error = dlerror()) != NULL)  {
        fprintf (stderr, "%s\n", error);
        exit(1);
    }
    printf ("%f\n", (*cosine)(2.0));
    dlclose(handle);
    return 0;
}
```

my_lib_test.so just contains a printf("text")

I want that another ELF executable program that loads this library, could load many other libraries with one only. I don't know if I explained it well.

Well, ok:
ELF executable that loads -> lib.so ->lib.so loads lib2.so, lib2.so etc. right? But with a bash script (I have a 64bit system):

```
#!/bin/sh
	cd /test/my_data/my_elf
	env LD_32_PRELOAD="/test/my_data/my_lib_test.so" ./my_elf &
	sleep 1
```
it happens like this:

```
Cannot open "/test/my_data/my_lib.so"
```
while if I type `./my_lib.so` on my shell it writes:

```
Segmentation fault (core dumped)
```


----------

