This is probably going to be embarrassing:
I am using library prelaoding in other projects, but I cannot get this minimal example to work:
weakref.h:
void f_weak() __attribute__((weak));
weakref.c:
#include <stdio.h>
#include "weakref.h"
void f_weak(){
printf("f_weak()\n");
fflush(stdout);
}
test_weakref.c:
#include <stdio.h>
#include "weakref.h"
int main(void)
{
if (f_weak) {
printf("main: f_weak()\n");
}
else {
printf("main: ---\n");
}
fflush(stdout);
return 0;
}
Here is what I do:
$ gcc weakref.c -shared -fPIC -o libweakref.so
$ nm libweakref.so | grep f_weak
0000000000000708 W f_weak
$ gcc test_weakref.c -o test_weakref
$ ./test_weakref
main: ---
$ LD_PRELOAD=./libweakref.so ./test_weakref
main: ---
The expected output of the last command is
main: f_weak()
What am I missing?