2

I've been dabbling in porting the old vms-empire game to zig, or at least to use zig build for it. After some cleanup i got to the phase where i'm able to use zig cc instead of just cc in the makefile to build it successfully.

I moved to phase where i try to create a build.zig file to build the project and it builds but crashes immediately with illegal hardware instruction.

I'm wondering what is the difference between zig cc and the compile phase of zig build for c-files.

(i'm doing this on intel mac and zig is version 0.13.0)

Current version with make can be found in GitHub

i'd like to build the project successfully with zig build

2
  • same happens on a dockerized archlinux Commented Aug 15, 2024 at 14:43
  • Post your build build.zig, or at least the parts where you configure your C bits. Commented Aug 16, 2024 at 9:09

1 Answer 1

2

Typically, "illegal hardware instruction" is caused because zig enables -fsanitize=undefined for C code by default. This can be disabled in the command line with -fno-sanitize=undefined or in zig build with exe.root_module.sanitize_c = false; in build.zig. Alternatively, the problem in C code triggering undefined behaviour can be identified and fixed with a debugger.

In this case, zig cc did not trigger the crash because your Makefile set -O2, which zig sees and automatically disables C sanitization. In zig, setting -OReleaseFast or -Doptimize=ReleaseFast disables C sanitization, and it mirrors this in zig cc by disabling C sanitization when an optimization level is set.

Documentation on this behaviour is here: https://github.com/ziglang/zig/wiki/FAQ#why-do-i-get-illegal-instruction-when-using-with-zig-cc-to-build-c-code

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.