Linking with static C lib

Hi, I have some C libs that I would like to link with my zig app. The docs seem to suggest all manner of C integration is possible. Some posts suggest this isn’t supported but I can’t see why this very useful feature would have been excluded. I can link a dynamic library but all attemps to persuade it to link a static lib have had it asking for an import lib. Anyone managed to make this work? Bob

Can you provide an example of the command line and inputs that you tried?

Linking against static libraries is supported and normally done by providing a -L pointing at the directory containing the static library, and -I pointing at the directory containing the corresponding header file(s).

This blog post contains an example of using zig build to build Redis, although note that if you’re using a recent untagged build of Zig, you will find that the build systems has changed its API, so some adjustments might be necessary.

I have a simple C file for testing with one function. I can get this to work letting zig build it or as a dll but not a static lib.

In build.zig I have:

exe.addLibraryPath(“E:/try/zig/basic/x64/Release”);
exe.addIncludePath(“E:/try/zig/basic/basic”);
exe.linkSystemLibrary(“basic.lib”);

If I run this I get:

E:\try\zig\hello>zig build run
LLD Link… error(link): DLL import library for -lbasic.lib not found
error: DllImportLibraryNotFound
error: hello…
error: The following command exited with error code 1:
E:\DevelopmentResources\Zig\zig-windows-x86_64-0.11.0-dev.1928+3169f0529\zig.exe build-exe E:\try\zig\hello\src\main.zig -lbasic.lib --cache-dir E:\try\zig\hello\zig-cache --global-cache-dir C:\Users\User\AppData\Local\zig --name hello -I E:\try\zig\basic\basic -L E:/try/zig/basic/x64/Release --enable-cache
error: the following build command failed with exit code 1:
E:\try\zig\hello\zig-cache\o\8a71195302e05f1b19768b76eb982981\build.exe E:\DevelopmentResources\Zig\zig-windows-x86_64-0.11.0-dev.1928+3169f0529\zig.exe E:\try\zig\hello E:\try\zig\hello\zig-cache C:\Users\User\AppData\Local\zig run

If I just put:
exe.linkSystemLibrary(“basic”);
I get:
error: lld-link: unknown file type: x64\Release\basic.obj

It looks like the build command is correct.

Assuming a msys2 dev environment. So the way I’ve done it was by adding to the build.zig file : exe.addObjectFile(“lib/libsomename.a”) . This static library was something I generate with gendef and dlltool from whatever dll I was pulling in…