The man page of gcc 6.3 says:
--wrap=symbol
Use a wrapper function for symbol. Any undefined reference to
symbol will be resolved to \
As StoryTeller told me, I ignored the "undefined reference" requirement which I already posted above:
... Any undefined reference to symbol will be resolved to "__wrap_symbol". Any undefined reference to "__real_symbol" will be resolved to symbol.
To use the --wrap
option I rearranged my code example like this:
main.c:
#include <stdio.h>
extern int foo();
extern int __real_foo();
int __wrap_foo() {
printf("wrap foo\n");
return 0;
}
int main () {
printf("foo:");foo();
printf("wrapfoo:");__wrap_foo();
printf("realfoo:");__real_foo();
return 0;
}
foo.c:
#include <stdio.h>
int foo() {
printf("foo\n");
return 0;
}
Then compile:
gcc main.c foo.c -Wl,--wrap=foo -o main
And the the amazing output after running ./main
:
foo:wrap foo
wrapfoo:wrap foo
realfoo:foo
The trick is (correct me if I am wrong) that the reference of foo()
and __real_foo()
is not defined at compile time. I. E. they have **undefined references" which is the requierement for the linker to link foo()
to __wrap_foo()
and __real_foo()
to foo()
.