Targeting a specific macOS version with `zig cc` doesn't work.
Zig Version
0.11.0-dev.1637+4e6f21e2c
Steps to Reproduce and Observed Behavior
$ echo '#include <stdio.h>' >hello.c
$ echo 'int main(int argc, char **argv) { (void)argc; (void)argv; printf("Hello, World!\n"); return 0; }' >>hello.c
$ MACOSX_DEPLOYMENT_TARGET=10.5 zig cc -target x86_64-macos-none -mmacosx-version-min=10.5 -W -Wall -s -O2 -o hello.darwin105 hello.c
$ otool -l hello.darwin105 | grep -F 10.5
(no output)
Expected Behavior
$ otool -l hello.darwin105 | grep -F 10.5
version 10.5
More expected lines in the output of otool -l hello.darwin105:
cmd LC_VERSION_MIN_MACOSX
cmdsize 16
version 10.5
sdk 10.10
This is a really straightforward fix. It means recognizing the clang argument and modifying the triple in CLI parsing.
If you want a workaround for the meantime, pass the os version with the -target parameter like this:
-target x86_64-macos.10.5-none
The suggested workaround doesn't work for me:
$ zig cc -target x86_64-macos.10.5-none -Werror=partial-availability -W -Wall -s -O2 -o hello.darwin105 hello.c
hello.c:1:10: fatal error: 'stdio.h' file not found
#include <stdio.h>
^~~~~~~~~
1 error generated.
I tried adding random -I... flags, but it doesn't work even after adding 3 of them.
Is there a workaround which actually works?
try linking libc with -lc
Adding -lc doesn't make any difference (as expected):
$ zig cc -target x86_64-macos.10.5-none -Werror=partial-availability -W -Wall -s -O2 -o hello.darwin105 hello.c -lc
hello.c:1:10: fatal error: 'stdio.h' file not found
#include <stdio.h>
^~~~~~~~~
1 error generated.
So is it actually possible to target a specific macOS version with zig cc?
@alichraghi -lc is enabled by default when using zig cc. Also -lc is enabled by default when targeting macOS.
macOS 10 is no longer supported by Apple, and therefore also no longer supported by Zig. You have to use one of the latest 3 versions or else your system is not being patched for security vulnerabilities, and likewise zig does not provide support for cross compiling to anything less than the latest 3 versions.
https://ziglang.org/download/0.10.0/release-notes.html#macOS
Thank you for the clarification on which macOS versions Zig can target.
You have to use one of the latest 3 versions or else your system is not being patched for security vulnerabilities,
I understand this: a system running an old, unpatched macOS version is more vulnerable than a system running a new, patched macOS version, and only the latest 3 versions are getting patches.
Therefore also no longer supported by Zig.
I don't think this Zig policy provides any security benefit. Targeting an old version V of macOS means that the program can run on version V and newer, including the newest 3 versions. Some security bugs have been fixed in newer versions of macOS, so the same program targeting old version V will run more securely on those new versions, without any changes to the program itself.
As a software developer, I want to compile my programs with relatively recent compilers, and I want to target both old and new systems with the same precompiled program binary. I don't want to dictate to my users when to upgrade their systems. (Of course, my recommendation to them is to upgrade as often as possible, to get all the security patches.) Thus, as of now, I'm not able to use the latest zig cc to target an old (minimum) version of macOS, so I have to use other compiler toolchains which let me.
this would be a discussion for another issue, but perhaps in the future compilation support for unsupported platforms could also be provided by 3rd-party modules
I can confirm that the following commands work in Zig 0.11.0-dev.1637+4e6f21e2c , and they target the respective version of macOS (as verified by the the output of otool -l prog | grep minos):
$ zig cc -target x86_64-macos.13-none -Werror=partial-availability -W -Wall -s -O2 -o prog preg.c
$ zig cc -target x86_64-macos.12-none -Werror=partial-availability -W -Wall -s -O2 -o prog prog.c
$ zig cc -target x86_64-macos.11-none -Werror=partial-availability -W -Wall -s -O2 -o prog prog.c
$ otool -l prog | grep minos
minos 11.0
Targeting a too old or too new version fails (as intended):
$ zig cc -target x86_64-macos.10.15-none -Werror=partial-availability -W -Wall -s -O2 -o prog preg.c
prog.c:1:10: fatal error: 'stdio.h' file not found
#include <stdio.h>
^~~~~~~~~
1 error generated.
$ zig cc -target x86_64-macos.14-none -Werror=partial-availability -W -Wall -s -O2 -o prog preg.c
prog.c:1:10: fatal error: 'stdio.h' file not found
#include <stdio.h>
^~~~~~~~~
1 error generated.
Please add a more descriptive error message. Something like:
$ zig cc -target x86_64-macos.10.15-none -Werror=partial-availability -W -Wall -s -O2 -o prog preg.c
fatal: unsupported version 10.15 in macOS target: x86_64-macos.14-none
info: supported macOS versions: 11, 12, 13
macOS 10 is no longer supported by Apple, and therefore also no longer supported by Zig.
I just tried to use Zig on Catalina and stumbled upon roadblock of not able to link it against MacOSX11.1.sdk with -mmacosx-version-min=10.15 even though it compiles fine.
I strongly disagree with this thinking and decisions to remove 10.15 target in Zig.
Why? 10.x macOS is important OS version that deserve to be supported. And this is why latest Xcode 14.3 supports building applications that target macOS 10.13.
https://developer.apple.com/support/xcode/