Linux proxy library (between two libraries)

Hello,

I need to create library which will be like proxy between two other libraries. Exactly I have one high level library which use low level library. I would like to create small lib that implements all functions from low-level lib; do some job and call real method from low-level library. And I don't known how to comile something like this, without problems with repeat function names.

Example:
void function(void) {
  do_something();
  function(); // low level library
}

I figured that I could wrap low level functions e.g.
void wrap_function(void) {
  function();
}

And then use it in proxy library:
void function(void) {
  wrap_function();
}

But when I'm trying to run this I'm getting Segmentation fault because functions are looping:
(gdb) bt
#0  nfc_version () at main.c:5
#1  0x00000000004011fb in wrap_nfc_version ()
#2  0x0000000000400ddd in nfc_version () at main.c:5
#3  0x00000000004011fb in wrap_nfc_version ()
#4  0x0000000000400ddd in nfc_version () at main.c:5
#5  0x00000000004011fb in wrap_nfc_version ()
#6  0x0000000000400ddd in nfc_version () at main.c:5
#7  0x00000000004011fb in wrap_nfc_version ()
#8  0x0000000000400ddd in nfc_version () at main.c:5
#9  0x00000000004011fb in wrap_nfc_version ()
#10 0x0000000000400ddd in nfc_version () at main.c:5
#11 0x00000000004011fb in wrap_nfc_version ()
#12 0x0000000000400ddd in nfc_version () at main.c:5
#13 0x00000000004011fb in wrap_nfc_version ()
#14 0x0000000000400ddd in nfc_version () at main.c:5
(...)

Do you have any ideas how to solve this problem ?
KrzysztofRosinskiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

 
Infinity08Commented:
>> I would like to create small lib that implements all functions from low-level lib; do some job and call real method from low-level library.

That's called a wrapper. Your wrapper library will have to use different function names than the ones from the existing library.

Your code will then make calls to the functions implemented in the wrapper library.
/* your library function (declared in library.h) : */

void fun();


/* your wrapper library function : */

#include "library.h"

void wrap_fun() {
  /* do something before ... */
  fun();
  /* do something after ... */
}



/* your calling code */

#include "wrap_library.h"

wrap_fun();

Open in new window

0
 
HappyCactusCommented:
If I understand correctly the problem, you want to develop a library that have exactly the same names that the "wrapped" one. You can't. In C you can't prevent name clashing, while you could in C++ by using namespaces.
But you could use a trick, by loading the wrapped library at runtime, using dlopen and dlsym to load the library and "link" each wrapper function with the wrapped one, by using functions pointer.
See the example here: http://www.opengroup.org/onlinepubs/009695399/functions/dlsym.html

Hope that helps.
0

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
 
KrzysztofRosinskiAuthor Commented:
Yep, this solution is correct, I finally did it yesterday evning and it is exacely what I need.
Thanks Guys.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.