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()

.

+3


source to share


3 answers


Add your implementation foo

to the source code. In this function, call bar

. The compiler should pick up your version foo

before the version in the library.



0


source


#include "foolib.h"

#define foo bar

void bar()
{
    //foo() replaced with bar()
}

      



As long as your #define statement appears after #include, it will not rename the function definition inside the library. This method is used all the time.

0


source


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.

0


source







All Articles