Skip to content

Instantly share code, notes, and snippets.

@fzakaria
Created September 10, 2023 02:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fzakaria/afefef95c4d6eb8235d538b7b57dc2c3 to your computer and use it in GitHub Desktop.
Save fzakaria/afefef95c4d6eb8235d538b7b57dc2c3 to your computer and use it in GitHub Desktop.
Order of linked libraries can affect symbol resolution
#include <stdio.h>
void f();
void a() {
puts("liba.so: hello world");
f();
}
#include <stdio.h>
void f() { puts("libaltf.so: goodbye world"); }
#include <stdio.h>
void f();
void b() {
puts("libb.so: hello world");
f();
}
void a();
void b();
int main() {
a();
b();
}
#include <stdio.h>
void f() { puts("libf.so: hello world"); }
example: example.c liba.so libb.so
gcc -o example -lb -la -laltf -lf -x c -L. example.c
libf.so: f.c
gcc -shared -o libf.so -x c f.c
libaltf.so: alternative-f.c
gcc -shared -o libaltf.so -x c alternative-f.c
liba.so: a.c libf.so
gcc -shared -o liba.so -L. -lf -x c a.c
libb.so: b.c libaltf.so
gcc -shared -o libb.so -L. -laltf -x c b.c
clean:
rm -f *.so example
.PHONY: clean
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment