I try to wrap a template function with the help of GNU's linker wrap option. The code looks like this:
// f.h
template<typename T>
void f(T t) {
}
// bar.h
void bar();
// bar.cpp
#include "bar.h"
#include "f.h"
void bar() {
f(42);
}
// test.cpp
extern "C" {
extern void __real__Z1fIiEvT_(int i);
void __wrap__Z1fIiEvT_(int i) {
__real__Z1fIiEvT_(i);
}
}
int main() {
bar();
}
The code shown above is linked with the following command:
g++ -Xlinker -wrap=_Z1fIiEvT_ -o test test.o bar.o
Unfortunately, this does not work and always the original function f is called instead of my wrapped version __wrap__Z1fIiEvT_. Do you see any mistakes I've made?
Edited: As advised, I append the output of nm here to make sure that the I haven't done any mistakes with the mangled name of the template function:
$ g++ -c bar.cpp -o bar.o
$ nm bar.o
0000000000000000 W _Z1fIiEvT_