Project Home
Project Home
Documents
Documents
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
Forum Topic - Choosing between libraries with same API at run-time: (4 Items)
   
Choosing between libraries with same API at run-time  
Hello,

I wish to write a program that at run-time can choose between libc's mqueue (mq_*) API and libmq.so's API.

Since they have exactly the same function names (mq_send, mq_receive, etc.), how can I link to both libraries but at 
runtime choose which actual implementation's code to execute?

The purpose of this application is so that the user can at run-time decide if they want to run a performance test with 
mq or mqueue.  

-asherk
RE: Choosing between libraries with same API at run-time  
You write youn own library, implement the API
mq_open()/mq_send()/mq_receive()/..., has your application link to your
own library.

During the initilize period, (or the first call of a mq_open() into your
library), you find out the 2 set of mq_* functions.

	dlopen("libc.so.3", ...)
	libc_mq_open = dlsym(..., "mq_open");
	...

	dlopen("libmq.so", ...)
	libmq_mq_open = dlsym(..., "mq_open");
	...

And they you decided how to switch between the 2.

-xtang

> -----Original Message-----
> From: Andrew Sherk [mailto:community-noreply@qnx.com] 
> Sent: Monday, February 02, 2009 12:03 PM
> To: ostech-core_os
> Subject: Choosing between libraries with same API at run-time
> 
> Hello,
> 
> I wish to write a program that at run-time can choose between 
> libc's mqueue (mq_*) API and libmq.so's API.
> 
> Since they have exactly the same function names (mq_send, 
> mq_receive, etc.), how can I link to both libraries but at 
> runtime choose which actual implementation's code to execute?
> 
> The purpose of this application is so that the user can at 
> run-time decide if they want to run a performance test with 
> mq or mqueue.  
> 
> -asherk
> 
> _______________________________________________
> OSTech
> http://community.qnx.com/sf/go/post21237
> 
> 
Re: Choosing between libraries with same API at run-time  
Actually, if you don't need dynamic switch. There is much easy way.

Write your test application normally, link it with libc.

If you run it directly, it use mq_* in libc. 

If you want to use libmq, try this:

 # LD_PRELOAD=/full/path/libmq.so ./mytestapp
Re: Choosing between libraries with same API at run-time  
ELF also specifies DT_FILTER and DT_AUXILIARY to help with this sort of
thing, but I don't think our runtime loader supports them at the moment.

On Mon, 2009-02-02 at 12:18 -0500, Xiaodan Tang wrote:
> Actually, if you don't need dynamic switch. There is much easy way.
> 
> Write your test application normally, link it with libc.
> 
> If you run it directly, it use mq_* in libc. 
> 
> If you want to use libmq, try this:
> 
>  # LD_PRELOAD=/full/path/libmq.so ./mytestapp
> 
> 
> _______________________________________________
> OSTech
> http://community.qnx.com/sf/go/post21240
>