Shared Lib help

Roger Oberholtzer roger
Mon May 17 11:59:14 PDT 2004


Vary simple: dlopen() and dlsym()

To load a module called, say, libmine.so.1.3, that wants a function called
funX, which takes as parameters one integer, one integer pointer, and a
char, and returns an int:

you would add to your code:

	#include <dlfcn.h>

        static void *handle = dlopen("libmine.so.1.3", RTLD_LAZY);

        static int (*_funX_) (int, int *, char);

	_funX_ = (int (*) (int, int *, char)) dlsym(handle, "funX");

	if (!_funX_) { some error stuff }


Then, to call this function:

	int result = _funX_(soneInt, &soneInt, 'a');

Obviously _funX_ can be whatever you want to call it. I would just not use
the same name as the function. Although I do not know that the loader is
confused by this, I would not waste time finding out as it would probably be
different on some other OS.

Here is a trick we use to check for a function that may or may not have
been linked to our program:


	handle = dlopen(0, RTLD_LAZY);

The '0' parameter to dlopen says to open the current process. Then you can
look foe a function. Then just use dlsym to find the functions of interest.

This works on Linux and most Unix versions. There is also a Windows
counterpart that does pertty much the same thing.


On Mon, 9 Feb 2004 01:11:00 -0600
Rick Sivernell <res005ru at verizon.net> wrote:

> Kurt or anyone else
> 
>    I have a shared lib that I have created, how do I load the lib with out
>    linking
> from the make file or is that possible. I have a program for my algorithms
> class and the PhD has writen a make file and I do not think we are allowed
> to modify it at all. Iwould like to use a shared lib and load the resource
> from the mainfile? Winders has such a api, doe linux ?
> 
> cheers
> 
> -- 
> Rick Sivernell
> Dallas, Texas  75287
> 972 306-2296
> res005ru at gte.net
> Gentoo Linux
> Registered Linux User #193859
> 
>    .~.
>   / v \
>  /( _ )\
>    ^ ^
> In Linux we trust!
> _______________________________________________
> Linux-users mailing list
> Linux-users at smtp.linux-sxs.org
> Unsubscribe/Suspend/Etc ->
> http://smtp.linux-sxs.org/mailman/listinfo/linux-users
> 


-- 
+????????????????????????????+???????????????????????????????+
? Roger Oberholtzer          ?   E-mail: roger at opq.se        ?
? OPQ Systems AB             ?      WWW: http://www.opq.se/  ?
? Erik Dahlbergsgatan 41-43  ?    Phone: Int + 46 8   314223 ?
? 115 34 Stockholm           ?   Mobile: Int + 46 733 621657 ?
? Sweden                     ?      Fax: Int + 46 8   302602 ?
+????????????????????????????+???????????????????????????????+



More information about the Linux-users mailing list