This issue I just created does a good job of describing what I ran into:
opened 11:53PM - 26 Jun 24 UTC
bug
### Zig Version
0.13.0
### Steps to Reproduce and Observed Behavior
I made an… example repo to reproduce this issue here:
https://github.com/haydenridd/zig-system-dependencies-bug
The issue is as follows:
In the example, there's a folder `sys_lib_folder` acting as a "system folder" that contains a pre-compiled static system library, in this case `libsys_lib.a`. I build this library from the folder `sys_lib`, it's a library with a single function `sysLibFunc` that returns 42.
So, let's say I want to link against this "system library" in a static library I'm creating. If we look at root `build.zig` I do that here:
``` zig
// Static library that depends on our "sys_lib" pre-compiled system library
const static_lib = b.addStaticLibrary(.{ .name = "staticlib", .target = target, .optimize = optimize });
static_lib.addCSourceFiles(.{
.files = &.{"static_library/staticlib.c"},
});
static_lib.addIncludePath(b.path("static_library"));
static_lib.installHeadersDirectory(b.path("static_library"), "staticlib", .{});
static_lib.addSystemIncludePath(.{ .cwd_relative = "/home/hayden/Documents/zig/system_includes_bug/sys_lib_folder/include" });
static_lib.addLibraryPath(.{ .cwd_relative = "/home/hayden/Documents/zig/system_includes_bug/sys_lib_folder/lib" });
static_lib.linkSystemLibrary("sys_lib");
b.installArtifact(static_lib);
```
This works no problem, the static library is built as expected. However, now let's say I have an executable that depends on this system library, I create that like so:
``` Zig
// Exe that links against our static library
const exe = b.addExecutable(.{ .name = "main", .target = target, .optimize = optimize, .link_libc = true });
exe.addCSourceFiles(.{ .files = &.{
"src/main.c",
} });
exe.linkLibrary(static_lib);
```
In `src/main.c` we can see I'm using both my function from my static library `staticLibFunc()`, and a function from my system library `sysLibFunc()`. If you try to compile this, you'll get two errors:
- Can't find `libsys_lib.a`
- Can't find `sys_lib.h`
Adding the following fixes the issue:
``` Zig
// Required for "exe" to be able to find system library path for libsys_lib.a:
exe.addLibraryPath(.{ .cwd_relative = "/home/hayden/Documents/zig/system_includes_bug/sys_lib_folder/lib" });
// Required for "exe" to be able to find system include path for sys_lib.h:
exe.addSystemIncludePath(.{ .cwd_relative = "/home/hayden/Documents/zig/system_includes_bug/sys_lib_folder/include" });
```
But this shouldn't be necessary right? Shouldn't `static_lib` propagate the system include path and library path to `exe`? Because it definitely propagates linking against the "system library" via `-lsys_lib` to `exe`. Propagating one but not the other seems like a bug...
### Expected Behavior
I expected the following lines in my example to be unnecessary:
``` Zig
// Required for "exe" to be able to find system library path for libsys_lib.a:
exe.addLibraryPath(.{ .cwd_relative = "/home/hayden/Documents/zig/system_includes_bug/sys_lib_folder/lib" });
// Required for "exe" to be able to find system include path for sys_lib.h:
exe.addSystemIncludePath(.{ .cwd_relative = "/home/hayden/Documents/zig/system_includes_bug/sys_lib_folder/include" });
```
By calling:
``` Zig
exe.linkLibrary(static_lib);
```
`exe` should automatically acquire the system include path and library path added to `static_lib`.
But, the TLDR:
Create a static library in build.zig
, link in some “system” library using:
static_lib.addSystemIncludePath(.{ .cwd_relative = "/some/system/path/some_lib_folder/include" });
static_lib.addLibraryPath(.{ .cwd_relative = "/some/system/path/some_lib_folder/lib" });
static_lib.linkSystemLibrary("some_lib");
Link against this library in your application like so:
exe.linkLibrary(static_lib);
Observe that you get an error from Zig looking like:
error: error: unable to find dynamic system library 'some_lib' using strategy 'paths_first'. searched paths:
/usr/local/lib64/libsome_lib.so
/usr/local/lib64/libsome_lib.a
/usr/local/lib/libsome_lib.so
/usr/local/lib/libsome_lib.a
/usr/lib/x86_64-linux-gnu/libsome_lib.so
/usr/lib/x86_64-linux-gnu/libsome_lib.a
/lib64/libsome_lib.so
/lib64/libsome_lib.a
/lib/libsome_lib.so
/lib/libsome_lib.a
/usr/lib64/libsome_lib.so
/usr/lib64/libsome_lib.a
/usr/lib/libsome_lib.so
/usr/lib/libsome_lib.a
/lib/x86_64-linux-gnu/libsome_lib.so
/lib/x86_64-linux-gnu/libsome_lib.a
Observe that the path you provided to your static library in static_lib.addLibraryPath(()
is nowhere to be found…
This can’t be intended behavior, right? Shouldn’t the static library propagate its library/system include paths up to an executable that’s linking against it?