Zig Version
0.14.1
Steps to Reproduce and Observed Behavior
You have application code:
main.c
int gccFunc(int val);
int main(void)
{
int v = gccFunc(1);
return 0;
}
And then static library code:
gcc.c -> libgcc.a
int gccFunc(int a)
{
return a + 10;
}
The static library is compiled into libgcc.a in build.zig, and then manually linked to the executable via linkSystemLibrary():
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const library_name = "gcc";
const libgcc_mod = b.createModule(.{
.target = target,
.optimize = optimize,
});
libgcc_mod.addCSourceFiles(.{
.files = &.{"lib/gcc.c"},
.flags = &.{},
});
const libgcc_lib = b.addLibrary(.{
.name = library_name,
.linkage = .static,
.root_module = libgcc_mod,
});
b.installArtifact(libgcc_lib);
const exe_mod = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libc = true,
});
exe_mod.addCSourceFiles(.{
.files = &.{"src/main.c"},
.flags = &.{},
});
exe_mod.addLibraryPath(b.path("zig-out/lib"));
exe_mod.linkSystemLibrary(library_name, .{
.needed = true,
.preferred_link_mode = .static,
.use_pkg_config = .no,
});
const exe = b.addExecutable(.{
.name = "system_lib_bug",
.root_module = exe_mod,
});
exe.step.dependOn(&libgcc_lib.step);
exe.verbose_link = true;
b.installArtifact(exe);
}
This will fail with the error:
error: ld.lld: undefined symbol: gccFunc
note: referenced by main.c:4 (src/main.c:4)
note: /home/hayden/Documents/zig/system-lib-bug/.zig-cache/o/a63c1390c1be2c14448d5a0f31a42c21/main.o:(main)
Examining the build-exe call you can see needed-lgcc is added correctly:
/home/hayden/.zvm/0.14.1/zig build-exe /home/hayden/Documents/zig/system-lib-bug/src/main.c -search_paths_first_static -needed-lgcc -ODebug -L /home/hayden/Documents/zig/system-lib-bug/zig-out/lib -Mroot -lc --verbose-link --cache-dir /home/hayden/Documents/zig/system-lib-bug/.zig-cache --global-cache-dir /home/hayden/.cache/zig --name system_lib_bug --zig-lib-dir /home/hayden/.zvm/0.14.1/lib/ --listen=-
However looking at the linker call, libgcc.a is missing!
ld.lld --error-limit=0 -mllvm -float-abi=hard --entry _start -z stack-size=16777216 --build-id=none --image-base=16777216 --eh-frame-hdr -znow -m elf_x86_64 -o /home/hayden/Documents/zig/system-lib-bug/.zig-cache/o/88b05d7ddc13501e1f580717076f3b01/system_lib_bug /usr/lib/gcc/x86_64-redhat-linux/14/../../../../lib64/crt1.o /usr/lib/gcc/x86_64-redhat-linux/14/../../../../lib64/crti.o -rpath /home/hayden/Documents/zig/system-lib-bug/zig-out/lib -L /usr/lib/gcc/x86_64-redhat-linux/14/../../../../lib64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 /home/hayden/Documents/zig/system-lib-bug/.zig-cache/o/a63c1390c1be2c14448d5a0f31a42c21/main.o /home/hayden/.cache/zig/o/ee9c7c2323795433f5cfe8074ac46f67/libubsan_rt.a --as-needed -lm -lpthread -lc -ldl -lrt -lutil /home/hayden/.cache/zig/o/198003ee6be6fa4a77cf81686a4c740a/libcompiler_rt.a /usr/lib/gcc/x86_64-redhat-linux/14/../../../../lib64/crtn.o
This seems to be directly related to the name "gcc", when changing the name in build.zig all works as expected:
const library_name = "not_gcc";
Perhaps there is some conflict given this is a standard name for a C standard library file?
Expected Behavior
libgcc.a should be valid to add to an executable using linkSystemLibrary().
Zig Version
0.14.1
Steps to Reproduce and Observed Behavior
You have application code:
main.c
And then static library code:
gcc.c -> libgcc.a
The static library is compiled into
libgcc.ainbuild.zig, and then manually linked to the executable vialinkSystemLibrary():This will fail with the error:
Examining the
build-execall you can seeneeded-lgccis added correctly:However looking at the linker call,
libgcc.ais missing!This seems to be directly related to the name "gcc", when changing the name in
build.zigall works as expected:Perhaps there is some conflict given this is a standard name for a C standard library file?
Expected Behavior
libgcc.ashould be valid to add to an executable usinglinkSystemLibrary().