C Enabling a library function call
I have the source code of the C library and its config file, I can rebuild it at any time. There is a library function foo () that I want to replace due to its wrong behavior. I don't have to edit the library source, only the config file can be changed. Is there a way to replace all calls from foo () with bar () with this constraint?
I tried using the macro definition:
#define foo bar
It doesn't help as it also replaces the function name (declaration and definition).
I found a similar thread: C ++ Replacing Functions with Macros . The problem is I cannot change the source and there is no conditional code. Any suggestions?
EDIT . A simple example to clarify the problem. Let's say a library has a utility function plus()
for adding two integers and a function minus()
for subtracting two integers, which it calls internally plus(a,-b)
. But since the function plus()
does not calculate the correct result, I wrote a function myplus()
that works well. Now the problem is how to get the library to be called myplus()
instead plus()
.
source to share
Assuming foo () is inside foo.c, you can do the following:
Instead of adding foo.c to your project, add foowrap.c, which looks like this:
#define __FOOWRAP__
#include "foo.c"
In the header where you made your original #define, do instead:
#ifndef __FOOWRAP__
#define foo bar
#endif
This also assumes that foo () is not called inside foo.c. If so, I think there is no way to fix the problem without modifying this original file.
source to share