I'm currently trying to use the GLFW library in zig. I know it's not the best way to go about things, but I'm currently just porting over a C++ program to zig to get a good feel for the language. I've tried to add it through the commands using --library glfw3 and using build.zig, but nothing seems to work. Most answers are for linux online, so i'm not able to get much help anywhere else.
#C library not detected when linked on Windows
1 messages · Page 1 of 1 (latest)
ive also tried using the build.zig file like this:
const exe = b.addExecutable(.{
.name = "Minecraft",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
const zstbi = b.dependency("zstbi", .{});
exe.root_module.addImport("zstbi", zstbi.module("root"));
exe.linkLibrary(zstbi.artifact("zstbi"));
exe.addIncludePath("C:/msys64/ucrt64/include/");
exe.addLibraryPath("C:/msys64/ucrt64/bin/");
exe.linkSystemLibrary("glfw3");
exe.linkSystemLibrary("opengl32");
exe.linkSystemLibrary("glu32");
b.installArtifact(exe);
i dont really understand this, this is my first time using C libraries with this
am i asking the wrong question?
How do you link in Windows libraries?
How do you link in C libraries?
How do you link in C libraries on Windows?
whats the error? just guessing, you may need to use exe.addIncludePath to the include path of glfw3, opengl32, glu32. i dont think its neccecary to add the path for msys64 (ucrt) paths, since the standard library should be done for you
C:/msys64/ucrt64/include/ is the include path for all of those
oh sorry i didnt read before sending, include path for headers (.h files), then library path to the dir containing .dll / .a files
maybe lib instead of bin, if it exists?
i did use the incorrect path there
but i think the main issue is idk how to use it after that
zig build?
i mean in the code
ah, right. you can use @cImport(...) (see https://ziglang.org/documentation/master/#toc-Import-from-C-Header-File) which can translate c headers to zig.
alternatively in the build system there is a translate-c step for this.
to use the translate-c version, it takes a .h file. in a few zig releases, this will be the preferred way of doing things and cImport will be removed.
const translate_c = b.addTranslateC(.{
.root_source_file = b.path("c-libraries.h"),
.target = target,
.optimize = optimize,
.link_libc = true,
});
translate_c.addIncludePath(...);
// ... configure the step ...
exe.root_module.addImport("c", translate_c.createModule());
usage
const c = @import("c");
idk how much trouble ill get into later cause of stupidity lol
oh i have both .a and .h files
.h (c header) is a definition of what symbols are in the .a (posix static library "archive") file
also something to consider is bindings for those libraries there are for sure nicer zig bindings that wrap the API to use slices and zig error types, i know one for glfw exists.
these libraries also build everything from source
i know there are nicer ones, but they change how things work
tho its reasonable to just use the c layer especially when starting, it builds understanding
and also zig's library manager is absolute garbage to work through at my level of understanding
its a very simple file downloader, needs much more tooling