Question: I have an application a part of which uses shared libraries. These libraries are linked at compile time.

At Runtime the loader expects the shared object to be in the `LD_LIBRARY_PATH` , if not found the entire application crashes with error "unable to load shared libraries".Note that there is no guarantee that client would be having the library, in that case I want the application to leave a suitable error message also the independent part should work correctly.

For this purpose I am using `dlsym()` and `dlopen()` to use the API in the shared library. The problem with this is if I have a lot of functions in the API, i have to access them Individually using `dlsym()` and ptrs which in my case are leading to memory corruption and code crashes.

Are there any alternatives for this?

Answer: 
Use below type of code 

```
Class DynLib
{
    /* All your functions */
    void fun1() {};
    void fun2() {};
    .
    .
    .
}

DynLib* getDynLibPointer()
{
    DynLib* x = new Dynlib;
    return x;
}

```

use `dlopen()` for loading this library at runtime.
and use `dlsym()` and call `getDynLibPointer()` which returns DynLib object.
from this object you can access all your functions jst as `obj.fun1()`..... 

This is ofcource a C++ style of struct method proposed earlier.
You are probably looking for some form of delay library load on Linux. It's not available out-of-the-box but you can easily mimic it by creating a small static stub library that would try to `dlopen` needed library on first call to any of it's functions (emitting diagnostic message and terminating if `dlopen` failed) and then forwarding all calls to it.

Such stub libraries can be written by hand, generated by project/library-specific script or generated by universal tool [Implib.so](https://github.com/yugr/Implib.so):

```
$ implib-gen.py libxyz.so
$ gcc myapp.c libxyz.tramp.S libxyz.init.c ...

```