Build system with nonstandard linker

I’m trying to build a windows executable using the build system, but I need to use a specialist linker. I’m sizecoding, so I would like to use Crinkler.

I presume the way to do that would be to b.addObject(), make the object file a dependency, and then run the linker as a system command. But I’m really struggling to implement that.

Could anyone point me in the right direction?

Hello @going-digital Welcome to ziggit :slight_smile:

For the simplest possible test.zig source you can create an object file by invoking zig build-obj and then use the linker of your choice.

> type test.zig
pub fn main() void {}
> zig build-obj -target native-windows -O ReleaseSmall -fsingle-threaded test.zig
> zig lld-link test.obj /subsystem:console /out:test.exe /entry:wWinMainCRTStartup /path/to/ntdll.lib

This invocation gave me a 2.6K test.exe
You can set your own entry point (see lib/std/start.zig) and you might need compiler_rt.lib and other libraries.
Zig is storing lib files in cache and displays linker invocation when called using:

> zig build-exe --verbose-link -target native-windows -O ReleaseSmall -fsingle-threaded test.zig
LLD Link... lld-link -ERRORLIMIT:0 -NOLOGO -STACK:16777216 -BASE:4194304 -MACHINE:X64 -OUT:test.exe -IMPLIB:test.lib test.exe.obj -SUBSYSTEM:console,6.0 -NODEFAULTLIB -ENTRY:wWinMainCRTStartup .cache/zig/o/72818dc8856a404aeabe68de258b13e8/c.lib .cache/zig/o/fd7227f31737214b46086b11c2c73538/compiler_rt.lib .cache/zig/o/a8c4c05ffa336cc34b3a6503a695ac3d/ntdll.lib

I would have liked to use the new build system - but maybe I can do that in future. For now, thanks to @dimdin above, I have a functional build which is a great start. OpenGL graphics and token synthesized audio in a 1107 byte executable!


Test executable


This is really cool!