Skip to content

Instantly share code, notes, and snippets.

@motiejus
Last active May 24, 2023 13:07
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 motiejus/ad06faea223f468475837b995c9b9269 to your computer and use it in GitHub Desktop.
Save motiejus/ad06faea223f468475837b995c9b9269 to your computer and use it in GitHub Desktop.
zig-cc and rpath
$ make
cc -shared -o x/libversioned.so.2 x/imported.c
zig cc dylib_client.c '-Wl,-rpath,$ORIGIN/x' -L x -l :libversioned.so.2 -o dylib_client.zig-cc
gcc dylib_client.c '-Wl,-rpath,$ORIGIN/x' -L x -l :libversioned.so.2 -o dylib_client.gcc
zig cc:
0x0000000000000001 (NEEDED) Shared library: [libversioned.so.2]
0x000000000000001d (RUNPATH) Library runpath: [$ORIGIN/x]
gcc:
0x000000000000001d (RUNPATH) Library runpath: [$ORIGIN/x:x]
0x0000000000000001 (NEEDED) Shared library: [x/libversioned.so.2]
int bar() { return 0; }
extern int foo();
extern int bar();
int main() {
return foo();
return bar();
}
int foo() { return 0; }
ZIG ?= zig
.PHONY: test
test: t-rpath.zig-cc t-rpath.clang-16 t-plain.clang-16 t-plain.zig-cc x/libfoo.so.2
@echo clang-16 rpath:
@readelf -d t-rpath.clang-16 | grep -E "runpath|foo|bar" | sort
@echo zig cc rpath:
@readelf -d t-rpath.zig-cc | grep -E "runpath|foo|bar" | sort
@echo clang-16 plain:
@readelf -d t-plain.clang-16 | grep -E "runpath|foo|bar" | sort
@echo zig cc plain:
@readelf -d t-plain.zig-cc | grep -E "runpath|foo|bar" | sort
RPATHFLAGS='-Wl,-rpath,$$ORIGIN/x' -L x -l :libfoo.so.2 x/libbar.so.2
PLAINFLAGS=-L x -l :libfoo.so.2
SHLIBS = x/libfoo.so.2 x/libbar.so.2
t-rpath.zig-cc: dylib_client.c $(SHLIBS)
$(ZIG) cc $< $(RPATHFLAGS) -o $@
t-rpath.clang-16: dylib_client.c $(SHLIBS)
clang-16 $< $(RPATHFLAGS) -o $@
t-plain.zig-cc: dylib_client.c $(SHLIBS)
$(ZIG) cc $< $(PLAINFLAGS) -o $@
t-plain.clang-16: dylib_client.c $(SHLIBS)
clang-16 $< $(PLAINFLAGS) -o $@
x/lib%.so.2: %.c
mkdir -p x
$(CC) -shared -o $@ $<
clean:
rm -f x/libfoo.so.2 x/libbar.so.2 t-*cc t-*clang-16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment