Zig: Cannot build-exe with object files and generate executable file on windows

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

I run zig.exe build-exe --object xxx.obj command, it cannot generate any executable file.

zig: master version

y:\zig\zig.exe build-obj --release-fast -target x86_64-windows-msvc --output-dir build\.objs\test\windows\x64\release\src --name main.zig src\main.zig

y:\zig\zig.exe build-exe --strip -target x86_64-windows-msvc --output-dir build\windows\x64\release --name test --object build\.objs\test\windows\x64\release\src\main.zig.obj

Build Dependencies...compiler_rt...Initialize...
bug contributor friendly os-windows stage2

Most helpful comment

Hmm, my apologies, this does seem to be broken, I dont know how I got around it yesterday.
Looking into it now

All 11 comments

I cannot reproduce this on master with the new CLI (on both Linux and Windows hosts), closing.

I tried version 0.7.0, the obj file can be generated normally, but the link to the executable file still fails, and there is no error output

[ 40%]: compiling.release src\main.zig
y:\zig\zig.exe build-obj -target x86_64-windows-msvc -O ReleaseFast -femit-bin=build\.objs\test\windows\x64\release\src\main.zig.obj src\main.zig
[ 60%]: compiling.release src\test.zig
y:\zig\zig.exe build-obj -target x86_64-windows-msvc -O ReleaseFast -femit-bin=build\.objs\test\windows\x64\release\src\test.zig.obj src\test.zig
[ 80%]: linking.release test.exe
y:\zig\zig.exe build-exe -target x86_64-windows-msvc --strip -femit-bin=build\windows\x64\release\test.exe build\.objs\test\windows\x64\release\src\main.zig.obj build\.objs\test\windows\x64\release\src\test.zig.obj
error: runv(y:\zig\zig.exe build-exe -target x86_64-windows-msvc --strip -femit-bin=build\windows\x64\release\test.exe build\.objs\test\windows\x64\release\src\main.zig.obj build\.objs\test\windows\x64\release\src\test.zig.obj) failed(1)

I execute the link command alone without any output.

y:\zig\zig.exe build-exe -target x86_64-windows-msvc --strip -femit-bin=build\windows\x64\release\test.exe build\.objs\test\windows\x64\release\src\main.zig.obj build\.objs\test\windows\x64\release\src\test.zig.obj

@waruqi
Can you point me to the example project you are using?
I tried this with basically the exact same commands both on linux (cross compiling for gnu windows) and on windows (compiling for msvc windows)

example project: https://github.com/xmake-io/xmake/tree/dev/tests/projects/zig/console

Please download xmake-latest.win64.exe install package to install xmake (dev version) from https://github.com/xmake-io/xmake/actions/runs/355512412

build test project on windows/msvc

$ git clone https://github.com/xmake-io/xmake.git 
$ cd xmake/tests/projects/zig/console
$ xmake f --zc=f:\xxx\zig.exe -c
$ xmake -rvD

Thanks, will take a look right now and reopen and investigate the bug if necessary

Ok, thanks.

Hmm, my apologies, this does seem to be broken, I dont know how I got around it yesterday.
Looking into it now

Unfortunately this looks like an LLD bug (I think somewhere in lld::link::coff, exit(1) is called)
Trying to verify this now somehow

@waruqi
Apparently we are not passing a default subsystem to the linker when calling into LLD, which causes it to error.
A separate bug in the compiler means the error from lld was not printed out.
If you need a temporary workaround, you can fix this by adding --subsystem console to the command (as well as -lkernel32, -lntdll for the winapi symbols used by the program).

I suppose those should be added by default as well.

I will fix the compiler to either use a default subsystem or emit a compiler error in cases when no zig file is compiled, but only objet files.

If you need a temporary workaround, you can fix this by adding --subsystem console to the command (as well as -lkernel32, -lntdll for the winapi symbols used by the program).

ok, I will try it first. thanks.

If you need a temporary workaround, you can fix this by adding --subsystem console to the command (as well as -lkernel32, -lntdll for the winapi symbols used by the program).

This temporary workaround works, thanks. --subsystem console -lkernel32, -lntdll

But when I tried to continue to build dynamic and static libraries, two other problems appeared.

undefined symbol: _DllMainCRTStartup when I use zig build-lib --dynamic to generate dynamic library on windows

https://github.com/ziglang/zig/issues/7065

It crashes when I use zig build-lib to generate static library for msvc on windows

https://github.com/ziglang/zig/issues/7066

Was this page helpful?
0 / 5 - 0 ratings