For someone in the future ever encounters something like this, it’s probably happening because the pre-built static library requires libstdc++
, zig would be usually configured to link libc++
not libstdc++
, not sure how to tell zig to link with libstdc++
probably you can’t without recompiling zig?
So tried to use std.zig.system.NativePaths
fn hasFileIn(dir_path: []const u8, file: []const u8) bool {
var dir = std.fs.openDirAbsolute(dir_path, .{}) catch return false;
defer dir.close();
dir.access(file, .{}) catch return false;
return true;
}
fn getObjSystemPath(
native_path: std.zig.system.NativePaths,
obj_full_file: []const u8,
) ![]const u8 {
for (native_path.lib_dirs.items) |lib_dir| {
const resolved_lib_dir = try std.fs.path.resolve(native_path.arena, &.{lib_dir});
if (hasFileIn(resolved_lib_dir, obj_full_file)) {
return try std.fs.path.join(native_path.arena, &.{ resolved_lib_dir, obj_full_file });
}
}
return error.FileNotFound;
}
then can be used like this with combination of a build option(make sure you’ve not called linkLibCpp
)
// b: *std.Build, target: std.Build.ResolvedTarget, exe: *std.Build.Step.Compile
const libstdcxx_names: []const []const u8 = &.{
"libstdc++.so",
"libstdc++.a",
};
const native_path = try std.zig.system.NativePaths.detect(b.allocator, target.result);
for (libstdcxx_names) |libstdcxx_name| {
const libstdcxx_path = getObjSystemPath(native_path, libstdcxx_name) catch continue;
exe.addObjectFile(.{ .cwd_relative = libstdcxx_path });
break;
}
basically this will just search the system library paths for libstdc++
, not sure how cross-compatible(for unix-like platforms) this solution is, but something if you really need also addObjectFile
can be your friend.