#Linker errors when linking Zig static lib with C app (MSVC)

1 messages · Page 1 of 1 (latest)

nova mauve
#

I have a zig project that I've built as a static library. I've manually written a C header and want to use it in C projects. But when building my C project with it, I get linker errors:

1>memview.lib(memview.lib.obj) : error LNK2019: unresolved external symbol ___chkstk_ms referenced in function atomic.Atomic.Atomic(u32).init
1>memview.lib(memview.lib.obj) : error LNK2019: unresolved external symbol __divti3 referenced in function io.writer.Writer(*io.fixed_buffer_stream.FixedBufferStream([]u8),error{NoSpaceLeft},(function 'write')).print__anon_8668
1>E:\Dev\third_party\doomgeneric\x64\Debug\doomgeneric.exe : fatal error LNK1120: 2 unresolved externals

It seems like ___chkstk_ms and __divti3 are compiler builtin functions that clang has and zig implements, but MSVC doesn't. I would expect zig to bundle all needed dependencies in the static lib, so this behavior is confusing to me. I have 2 questions:

  1. Does what I'm doing even make sense? I would think so, since otherwise I'm not sure how any other language could bind to Zig libraries.
  2. Is this a bug in the zig compiler?
  3. Any recommendations for manually resolving the linker errors? I'm not sure where to get those functions for the MSVC compiler as part of a static lib.
nova mauve
#

When building the same library with gcc on windows (mingw), I get different linker errors:
```/usr/lib/gcc/x86_64-pc-cygwin/11/../../../../x86_64-pc-cygwin/bin/ld: ./libmemview.lib(E:/Dev/third_party/doomgeneric/lib/memview/zig-cache/o/75f08ab1a93426a89c39c298a8aebe61/memview.lib.obj):memview:(.text+0x49aa): undefined reference to _tls_index' /usr/lib/gcc/x86_64-pc-cygwin/11/../../../../x86_64-pc-cygwin/bin/ld: ./libmemview.lib(E:/Dev/third_party/doomgeneric/lib/memview/zig-cache/o/75f08ab1a93426a89c39c298a8aebe61/memview.lib.obj):memview:(.text+0x49e6): undefined reference to _tls_index'
/usr/lib/gcc/x86_64-pc-cygwin/11/../../../../x86_64-pc-cygwin/bin/ld: ./libmemview.lib(E:/Dev/third_party/doomgeneric/lib/memview/zig-cache/o/75f08ab1a93426a89c39c298a8aebe61/memview.lib.obj):memview:(.text+0x4bbe): undefined reference to `_tls_index'

So it does seem like Zig is omitting some necessary symbols.
#

Turning off LTO when generating the zig static lib didn't seem to make a difference in my case though.

lusty cape
# nova mauve I have a zig project that I've built as a static library. I've manually written ...

I would expect zig to bundle all needed dependencies in the static lib

in general, static libraries don't work like this and its up to you to specify external dependencies at link time

when building with zig/clang it will automatically link with compiler_rt, but msvc doesn't use the same function names

Does what I'm doing even make sense? I would think so, since otherwise I'm not sure how any other language could bind to Zig libraries.

it makes sense but you don't strictly need to do it like this unless you have other reasons for wanting msvc

zig also works as a frontend for clang that properly supports cross compilation, so you can use it as a build system and compiler for c or c++ projects

Any recommendations for manually resolving the linker errors? I'm not sure where to get those functions for the MSVC compiler as part of a static lib.

zig doesn't ship binary libraries, you can find the source for zig's implementation of those functions in lib/compiler_rt/ but to find the actual library you'd have to go searching through the cache in AppData/Local/zig/

if you have clang installed in visual studio, you'll have the llvm implementation somewhere in your install folder that should work

nova mauve
#

in general, static libraries don't work like this and its up to you to specify external dependencies at link time
Ah yeah I misspoke here, I guess I meant I expected internal dependencies like this to be included since I've never needed to link with these 2 functions explicitly before since they seem to be provided by the compiler.

I do have the whole project building with Zig, but wanted to try using a different compiler to see if that would work.

I will look into manually linking with the llvm libs to try to resolve this, thanks!

lusty cape
#

they are from the compiler's runtime library, which is an external dependency (just not one that you have to specify normally because when you use the same frontend to compile and link it will be automatic)