> Hi there!
> Where can I find example code and example linker commands
> for using dynamically (run-time) loaded libraries for
> gcc - compiler / gcc - linker or ld - linker on Linux systems
For gcc link with the flags -shared -fpic (or -fPIC), i.e.
gcc -shared -fpic -o LIBRARY.so LIBRARY.o
Quote:> Are there any windows like commands like
> FreeLibrary(dllname) and
> GetProcAddress(HINSTANCE,LPCSTR) ?
I would guess the corresponding functions might be
void *dlopen (const char *filename, int flag);
int dlclose (void *handle);
void *dlsym(void *handle, char *symbol);
Additionally, you have
const char *dlerror(void);
Have a look at the man page to find out about all the*details. Here's
some simple example code that should get you started:
handle = dlopen( "LIBRARY.so", RTLD_NOW );
if ( handle == NULL )
printf( "Can't open library: %s\n", dlerrer( ) );
/* Now get the address of the function or variable you need... */
my_function = dlsym( handle, "MY_FUNCTION_NAME" );
if ( dlerror != NULL )
printf( "Can't load function: %s\n", dlerror( ) );
/* ... Use the function (you may need some casts to keep the compiler
from issuing warnings) and when you're done with the library do: */
dlclose( handle );
One problem you might get is to specify the correct path for the library.
If you don't want to hard-code the full path where the library resides
into your program and you also don't want to install it in e.g. /usr/lib
or /usr/local/lib (where the loader will automatically look for it, at
least after running ldconfig) you should set the environment variable
LD_LIBRARY_PATH to the path of the location of the library before trying
to start the program.
_ _____ _____
_ | | | | | | AG Moebius, Institut fuer Molekuelphysik
| |_| | | | | | Fachbereich Physik, Freie Universitaet Berlin
\___/ens|_|homs|_|oerring Tel: ++49 (0)30 838 - 53394 / FAX: - 56046