Zig: Use `-target x86_64-macosx-gnu` will run failed on macOS

Created on 9 Jul 2020  路  3Comments  路  Source: ziglang/zig

I use -target x86_64-macosx-gnu to build static library and executable program, the generated program will run failed on macOS.

[ 37%]: ccache compiling.debug src/test.zig
/usr/local/bin/zig build-obj -target x86_64-macosx-gnu --output-dir build/.objs/testlib/macosx/x86_64/debug/src --name test.zig src/test.zig
checking for the zig compiler (zc) ... zig
[ 50%]: ccache compiling.debug src/main.zig
/usr/local/bin/zig build-obj -target x86_64-macosx-gnu --output-dir build/.objs/test/macosx/x86_64/debug/src --name main.zig src/main.zig
[ 62%]: archiving.debug libtestlib.a
/usr/local/bin/zig build-lib --output-dir build/macosx/x86_64/debug --name testlib --object build/.objs/testlib/macosx/x86_64/debug/src/test.zig.o
[ 87%]: linking.debug test
/usr/local/bin/zig build-exe -Lbuild/macosx/x86_64/debug -ltestlib -target x86_64-macosx-gnu --output-dir build/macosx/x86_64/debug --name test --object build/.objs/test/macosx/x86_64/debug/src/main.zig.o
[100%]: build ok!
ruki-2:static_library ruki$ xmake r
dyld: Symbol not found: __tlv_bootstrap
  Referenced from: /Users/ruki/projects/personal/xmake/tests/projects/zig/static_library/build/macosx/x86_64/debug/test
  Expected in: /Users/ruki/projects/personal/xmake/tests/projects/zig/static_library/build/macosx/x86_64/debug/test
 in /Users/ruki/projects/personal/xmake/tests/projects/zig/static_library/build/macosx/x86_64/debug/test
error: execv(/Users/ruki/projects/personal/xmake/tests/projects/zig/static_library/build/macosx/x86_64/debug/test) failed(-1)

But it works when I remove -target x86_64-macosx-gnu.

/usr/local/bin/zig build-obj --output-dir build/.objs/test/macosx/x86_64/debug/src --name main.zig src/main.zig
checking for the zig compiler (zc) ... zig
[ 50%]: ccache compiling.debug src/test.zig
/usr/local/bin/zig build-obj --output-dir build/.objs/testlib/macosx/x86_64/debug/src --name test.zig src/test.zig
[ 62%]: archiving.debug libtestlib.a
/usr/local/bin/zig build-lib --output-dir build/macosx/x86_64/debug --name testlib --object build/.objs/testlib/macosx/x86_64/debug/src/test.zig.o
[ 87%]: linking.debug test
/usr/local/bin/zig build-exe -Lbuild/macosx/x86_64/debug -ltestlib --output-dir build/macosx/x86_64/debug --name test --object build/.objs/test/macosx/x86_64/debug/src/main.zig.o
[100%]: build ok!
ruki-2:static_library ruki$ xmake r
Hello, world - 2!
bug os-macos upstream

Most helpful comment

This is due to LLD's MachO linker code being insufficiently maintained for years. Recently, 2 things have changed.

  1. There is a new team of maintainers working on LLD MachO linker code. They have not improved things yet with LLVM 11 release coming up but hopefully with LLVM 12 things will be improved.
  2. @kubkon has become interested in working on zig's self-hosted macho linker code. Depending on how far he gets, it may actually be the thing that solves this issue before (1). That said, Jakub's time is his own, and it would be unfair to set unreasonable expectations on him. This code is written in zig so we may see some other contributors enter this space too, if all goes well.

Just a note that things have picked up recently as seen in #6476 and we can generate basic functional MachO executables from any target, so if I/we manage to keep the pace up, point 2. is not unreachable (pan not intended).

All 3 comments

This is due to LLD's MachO linker code being insufficiently maintained for years. Recently, 2 things have changed.

  1. There is a new team of maintainers working on LLD MachO linker code. They have not improved things yet with LLVM 11 release coming up but hopefully with LLVM 12 things will be improved.
  2. @kubkon has become interested in working on zig's self-hosted macho linker code. Depending on how far he gets, it may actually be the thing that solves this issue before (1). That said, Jakub's time is his own, and it would be unfair to set unreasonable expectations on him. This code is written in zig so we may see some other contributors enter this space too, if all goes well.

This is due to LLD's MachO linker code being insufficiently maintained for years. Recently, 2 things have changed.

  1. There is a new team of maintainers working on LLD MachO linker code. They have not improved things yet with LLVM 11 release coming up but hopefully with LLVM 12 things will be improved.
  2. @kubkon has become interested in working on zig's self-hosted macho linker code. Depending on how far he gets, it may actually be the thing that solves this issue before (1). That said, Jakub's time is his own, and it would be unfair to set unreasonable expectations on him. This code is written in zig so we may see some other contributors enter this space too, if all goes well.

Just a note that things have picked up recently as seen in #6476 and we can generate basic functional MachO executables from any target, so if I/we manage to keep the pace up, point 2. is not unreachable (pan not intended).

Duplicate of #3295

Was this page helpful?
0 / 5 - 0 ratings