Compile.zig: Fix static->dynamic library linkage #25383
Open
+13
−5
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I think this should solve this issue: #19341
Thanks to @andrewrk for pointing me to the relevant portion of
Compile.zig
to find the cause (and for pointing out the resultant issue in the archive file).Reproducing the issue
dynamic-lib
, fromdynamic.c
exporting a functionvoid foo()
static-lib
, fromstatic.c
exporting a functionvoid bar() { foo(); }
static_lib.linkLibrary(dynamic_lib);
static-lib
anddynamic-lib
lld
while processing filelibstatic-lib.a
Explanation of the issue
The error occurs because the archive file
libstatic-lib.a
contains symbols from the dynamic librarylibdynamic-lib.so
, which is not valid for an archive file (static library).The problematic command is the
build-lib
command to build the archive, which includes the.so
file as a source:Note that both the
static.c
andlibshared-lib.so
files are given as position arguments to thebuild-lib
command.After these changes:
The
.so
file isn't listed, the error message does not appear, and the executable is still properly linked.System Libraries
The above shows the issue with user-defined libraries, but this occurs with system libraries as well (as pointed out in the linked issue). To view a reproduction, clone https://github.com/zig-gamedev/zglfw/ (referenced from this comment) and build with Zig 0.14.1.
Testing
I only have a Linux machine to test on; not sure if there would be any linker-related differences on Mac or Windows that might cause issues.