Skip to content

Instantly share code, notes, and snippets.

@pczarn
Created January 30, 2020 03:05
Show Gist options
  • Save pczarn/ecee491533a19b0a4ef4f43785f8badc to your computer and use it in GitHub Desktop.
Save pczarn/ecee491533a19b0a4ef4f43785f8badc to your computer and use it in GitHub Desktop.
weak and strong symbols
clang -Wall -fPIC -c ctest1.c ctest2.c
clang -shared -Wl,-soname,libctest.so.1.0 -o libctest.so.1.0 *.o
clang -fPIC prog.c -Wl,-rpath,. libctest.so.1.0 -o prog1
./prog1 > result1
clang prog.c ctest*.c -o prog2
./prog2 > result2
void ctest1(char **s)
{
*s = "from first";
}
void ctest2(char **s)
{
*s = "from second";
}
#include <stdio.h>
__attribute((weak)) void ctest1(char** s) {
*s = "from weak";
}
void ctest2(char**);
int main()
{
char *x;
ctest1(&x);
printf("result=%s\n",x);
ctest2(&x);
printf("result=%s\n",x);
return 0;
}
result=from weak
result=from second
result=from first
result=from second
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment