-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
macos mixed C/C++ and Zig builds fail #7697
Comments
which macOS version? |
Using the latest Big Sur on both x86 and ARM. |
yes, these notable changes in how Apple wants software built since Catalina:
since Big Sur:
I'm impressed you've been able to get builds working (prior to this issue) using system libs, system frameworks, 3rd-party libs, etc with relatively minimal The exact problem to that compile error is that zig forces use of bundled macos The problem however, is we currently have no easy way with build.zig or zig command line to stop insertion of those bundled includes on the command line. Do note this only solves the compile issue. It will not solve linking issues. We would still be forced to use Here's how I tweaked the command-line to make
and this produces the following search with
|
Many thanks for the workaround.
I am guessing this means there is no way to solve this from build.zig. Is there a fix planned when using the bundled headers? Perhaps an option to drop back to using the system headers like 0.7.0 did as a temporary hack is possible somehow? |
nothing clear yet. One thing that should be possible right now, and I know it sounds like "but why?" ... but you can setup these 3rd party libs that build against frameworks, to build with Xcode's toolchain. Hopefully any @cImport you do doesn't deeply include any frameworks. So basically build .zig, and build any .c stub/interop with This approach might work out... thinking about it we could have a kit that adds xcode toolchain support to build.zig in the long run. Needless to say, this kit would be for macos-hosted builds only, and nothing like the out-of-the-box convenience that zig's cross-compilation offers. |
Gotcha. I think for the time being since 0.7.0 works like a champ on ARM and x86 I’ll stick with it for macOS builds and use master for Linux. I like to have a good, workable version of zig to give to people that are interested in learning it and as long as we have 0.7.0 working so nicely all should be well ;) |
FYI @prime31, these are the current macOS workarounds I have in sokol-zig to add the /usr/include and frameworks-directory: https://github.com/floooh/sokol-zig/blob/ec0f1fe5452c560a1e489683cc726636ff0587bd/build.zig#L6-L12 In addition to this, the system-linker-hack must be used ( This is with macOS Big Sur 11.2 Beta, Xcode 12.2 and Zig 0.7.1 |
@floooh oddly, on latest non-beta Big Sur (11.1) with Xcode 12.3 that fails for me on x86 (didn't try ARM). I pulled your repo and grabbed zig master and it results in a linker error oddly enough.
That looks just like the norma LLD linker error but |
Hmm |
Same error @prime31 reports. Using the linker hack in my
macOS: Big Sur 11.1 (20C69) |
@floooh |
@mikdusan installed via homebrew with --HEAD, but not updated for a few weeks:
zig version prints:
clang --version prints:
The macOS SDK is MacOSX11.0.sdk |
Unfortunately, if you link against any framework on macOS using the LLVM linker, you'll be greeted by a segfault, for which the only workaround currently is to use the system linker hack. In terms of the framework headers @prime31, @mikdusan is spot on: you now need to specify the sysroot manually with the contents of |
Hmm, that looks worrying. I can confirm what you are seeing is an actual system linker reported error and not LLD's. I'll need to investigate further but it looks like perhaps something changed with Xcode 12.3. Did you try with CLT instead? |
I can confirm this is a bug in master, and #7719 fixes it. @prime31 and @floooh, with this fix I was able to compile |
@prime31 just going to drop the const builtin = @import("builtin");
const std = @import("std");
const Builder = std.build.Builder;
// This works on host Big Sur 11.1 w/ Xcode 12.3, with zig also built with Xcode 12.3 .
//
// NOTE: when PR #7714 https://github.com/ziglang/zig/pull/7714 is merged, this will also
// work with zig continuous integration macos tarballs posted on Zig website. Until then,
// those tarballs are subject to issue #7569 https://github.com/ziglang/zig/issues/7569
// and Big Sur users will need to build their own zig from sources w/ SDK 11.1 .
//
// -- mikdusan
pub fn linkArtifact(b: *Builder, exe: *std.build.LibExeObjStep, target: std.build.Target, comptime prefix_path: []const u8) !void {
exe.linkLibC();
var c_flags = std.ArrayList([]const u8).init(b.allocator);
try c_flags.append("-std=c99");
switch (std.Target.current.os.tag) {
.macos => {
try c_flags.append("-ObjC");
try c_flags.append("-fobjc-arc");
// make verbose (note: contents will only be shown on failure as per `zig build`)
try c_flags.append("-v");
// LLD does not support **framework** text ABI (.tbd) files.
// This forces zig to use system `ld` instead of embedded `lld`.
//
// NOTE: PR #7715 https://github.com/ziglang/zig/pull/7715 was merged
// to support environ from Builder. Prior to merge, using `env_map` has no effect.
try b.env_map.set("ZIG_SYSTEM_LINKER_HACK", "1");
// NOTE: PR #7719 https://github.com/ziglang/zig/pull/7719 was merged
// the following code is no longer required.
if (false) {
const sdk_dir = try std.zig.system.getSDKPath(b.allocator);
const fwk_dir = try std.mem.concat(b.allocator, u8, &[_][]const u8 { sdk_dir, "/System/Library/Frameworks" });
const inc_dir = try std.mem.concat(b.allocator, u8, &[_][]const u8 { sdk_dir, "/usr/include" });
try c_flags.appendSlice(&[_][]const u8{ "-isystem", inc_dir });
try c_flags.appendSlice(&[_][]const u8{ "-F", fwk_dir });
exe.addFrameworkDir(fwk_dir);
}
exe.linkFramework("Foundation");
exe.linkFramework("Cocoa");
exe.linkFramework("Quartz");
exe.linkFramework("QuartzCore");
exe.linkFramework("Metal");
exe.linkFramework("MetalKit");
exe.linkFramework("OpenGL");
exe.linkFramework("Audiotoolbox");
exe.linkFramework("CoreAudio");
exe.linkSystemLibrary("c++");
},
.linux => {
exe.linkSystemLibrary("GL");
exe.linkSystemLibrary("GLEW");
exe.linkSystemLibrary("X11");
},
else => {},
}
exe.addIncludeDir(prefix_path ++ "src/deps/sokol");
exe.addCSourceFile(prefix_path ++ "src/deps/sokol/compile_sokol.c", c_flags.items);
} |
The last working zig version was 0.7.0 for mixed builds on macos. There have been various different errors since then but it seems the error has now stabilized so I figure I'll report it.
The test project is available here. Full steps to repro:
git clone --recursive https://github.com/prime31/zig-upaya/
cd zig-upaya
zig build empty
The resulting error seems to be a missing include path that has popped up after 0.7.0:
The text was updated successfully, but these errors were encountered: