#Problems linking a zig .obj with link.exe; automatic call to '__main' inserted when exporting 'main'

1 messages · Page 1 of 1 (latest)

sweet terrace
#

When I compile a file called test.zig

export fn main() void {}

With 'zig build-obj test.zig', as well as compile compiler_rt/stack_probe.zig (for __chkstk_ms) in the same manner, and then try to link that with msvc's link.exe, against kernel32.lib, libcmt.lib, user32.lib, ntdll.lib and vcruntimed.lib, I get 'unresolved external symbol __main'. When I look at the disassembly of the object file exported by zig, there is indeed a call to '__main' inserted at the top of the main function.

So, my main question: why is this? What can I do about it?

And bonus questions :): I'd rather not compile every compiler_rt file manually when needed for e.g. memcpy, memset, __chkstk_ms. Can the zig compiler not use these functions automatically?
EDIT: and, why is there a test.obj and a test.obj.obj created?

zenith lava
#

I believe that if you want to use the system entry point, you have to call it one of the appropriate names. (See start.zig.)
Otherwise, just use pub fn main() instead.

#

Not sure why you end up with __main - might be because it clashes with Zig's main or something, so it renamed it - but there may be bugs related to that?
Not sure what you mean about compiling compiler_rt's stuff separately. That just sounds like you're barking up a strange tree. 😄

sweet terrace
#

By system entry point, you mean mainCRTStartup? I'm not sure what you mean. What I want to achieve is: instead of letting zig compile and link my hello world, I want zig to only compile and export a .obj, and link that myself.

#

Well, without compiling and linking some compiler_rt stuff, there were unresolved externals (e.g. __chkstk_ms, which isn't defined in some windows .lib). I don't know a lot about zig, but my guess is that zig was first linking against these functions from some MinGW library, but since these aren't available in MSVC, they added their own implementations?

zenith lava
#

So I'd probably use pub fn main + zig build-obj.
Though, export fn my_main + zig build-obj may also work for you, if you want to do something entirely custom.