dynamically loaded libs

dynamically loaded libs

Post by andr » Tue, 09 Apr 2002 23:25:25



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

and for

cc - compiler (HP) / ld -linker on HP-UX / HP9000/700 ?

Are there any windows like commands like
LoadLibrary(dllname)
FreeLibrary(dllname) and
GetProcAddress(HINSTANCE,LPCSTR) ?

Thanks very much in advance,
Andre

 
 
 

dynamically loaded libs

Post by Bill Medlan » Tue, 09 Apr 2002 23:29:25



Quote:> 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

> and for

> cc - compiler (HP) / ld -linker on HP-UX / HP9000/700 ?

> Are there any windows like commands like
> LoadLibrary(dllname)
> FreeLibrary(dllname) and
> GetProcAddress(HINSTANCE,LPCSTR) ?

man dlopen, dlclose, dlerror, dlsym

Bill

 
 
 

dynamically loaded libs

Post by Christia » Tue, 09 Apr 2002 23:40:55



Quote:> 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

> and for

> cc - compiler (HP) / ld -linker on HP-UX / HP9000/700 ?

> Are there any windows like commands like
> LoadLibrary(dllname)
> FreeLibrary(dllname) and
> GetProcAddress(HINSTANCE,LPCSTR) ?

On HP-UX, use shl_load, shl_unload, shl_findsym APIs
On Linux, use dlopen, dlclose, dlsym APIs

When linking, use -ldl, at least on Linux.

 
 
 

dynamically loaded libs

Post by Jens.Toerr.. » Tue, 09 Apr 2002 23:54:34



> 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
> LoadLibrary(dllname)
> 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:

void *handle;
void *my_function;

handle = dlopen( "LIBRARY.so", RTLD_NOW );
if ( handle == NULL )
{
    printf( "Can't open library: %s\n", dlerrer( ) );
    exit( EXIT_FAILURE;

Quote:}

/* 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( ) );
    exit( EXIT_FAILURE;

Quote:}

/* ... 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.
                                      Regards, Jens
--
      _  _____  _____

  _  | |  | |    | |          AG Moebius, Institut fuer Molekuelphysik
 | |_| |  | |    | |          Fachbereich Physik, Freie Universitaet Berlin
  \___/ens|_|homs|_|oerring   Tel: ++49 (0)30 838 - 53394 / FAX: - 56046

 
 
 

dynamically loaded libs

Post by Natarajan Krishnaswa » Wed, 10 Apr 2002 04:52:11



> On HP-UX, use shl_load, shl_unload, shl_findsym APIs
> On Linux, use dlopen, dlclose, dlsym APIs

I believe HP-UX has supported the interfaces from dlfcn.h since HP-UX
11.00(?), as part of the UNIX98 support, though I heard of problems
early on (such as the interfaces not being available for 32-bit code
w/o a patch).

N.

 
 
 

1. Dynamically Loading Lib in C++

I have been trying to write a dynamic loadable library that will load
into a C++ daemon I wrote.

I have successfully written and loaded another shared lib, and loaded it
just fine into a pure C program as well as find the symbol for the
function I was trying to access.  Once I recompile this lib and program
with g++ (as opposed to gcc) I can load the lib with 'dlopen()', but
'dlsym()' can't find the function symbol.

I figured it might be name mangling, but when I include 'dlfcn.h' as
extern "C" I still have the problem.  I'm sure that it is related to
name mangling, but I don't know what else to try.

I don't have the option of rewriting my other program and other lib in
C, so does anyone know of this problem, and how to get around it?

Regards, Kyle

Sent via Deja.com http://www.deja.com/
Before you buy.

2. SCO libsocket error with FastTrack Server

3. Slowdown when dynamically loading a shared lib

4. apache 1.3.1 vs mod_perl vs modified mod_userdir

5. can't load library /usr/lib/lib?????

6. Solaris socket port... where is definition of sockaddr_in?

7. /usr/lib/gcc-lib/i386-linux and /usr/lib/gcc-lib/i486-linux

8. scsi controller with ultra5

9. address space problem on dynamically linked libs

10. newbie, dynamically linked lib search path?

11. Cfront & Dynamically linked libs

12. dynamically linked static libs and 0 byte frame buf's in redhat

13. Anyone Building C++ Dynamically Linked Libs under Solaris?