Wrapping C++ functions with GNU linker
You need to either also extern "C"
the function you want to wrap (if that's possible) or you need to wrap the mangled name, e.g., __wrap__Z3foov
and then pass --wrap=_Z3foov
to the linker.
Getting the underscores right is a little tricky. This works for me:
$ cat x.cc
#include <iostream>
using namespace std;
int giveMeANumber();
int main() {
cerr << giveMeANumber() << endl;
return 0;
}
$ cat y.cc
int giveMeANumber() {
return 0;
}
extern "C" int __wrap__Z13giveMeANumberv() {
return 10;
}
$ g++ -c x.cc y.cc && g++ x.o y.o -Wl,--wrap=_Z13giveMeANumberv && ./a.out
10