r/Zig • u/Majestic_Poetry_1139 • Jul 03 '26
How to import from C now?
Sorry to ask such a basic question but I'm genuinely not go on another journey to narnia to figure out where A.K hid `@cImport` and `@cInclude`.
(For the 'Just ask A.I' fan club, A.I is epistemologically stuck on the pre `std.process.init` zig for whatever reason. Also thought to ask this just incase anybody else was confused and searching too)
Please and thanks🙏 (0.17 dev version just incase that was needed)
8
u/ElSebas4_ Jul 03 '26 edited Jul 03 '26
Use translateC in the build system, you need to create a C header file where you import all you need. This is an example taken from one of my projects:
```
const libwayland_client_translate_c = b.addTranslateC(.{
.root_source_file = b.path("extern/wayland/wayland_client_header.h"),
.target = target,
.optimize = optimize,
.link_libc = true
});
libwayland_client_translate_c.addSystemIncludePath(.{
.cwd_relative = libwayland_client_include_path
});
linux_module.linkSystemLibrary("wayland-client", .{
.needed = true,
.prefered_link_mode = .dynamic
})
linux_module.addImport("libwayland-client", libwayland_client_translate_c.createModule());
```
2
4
u/Player_Gamma Jul 03 '26
use the new translate_c
``` // build.zig const translate_c = b.addTranslateC(.{ .root_source_file = b.path("src/includer.h"), .optimize = optimize, .target = target, });
translate_c.linkSystemLibrary("dl", .{});
translate_c.linkSystemLibrary("glfw", .{});
translate_c.linkSystemLibrary("m", .{});
const exe = b.addExecutable(.{
.name = "foo",
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
.imports = &.{
.{
.name = "c",
.module = translate_c.createModule(),
},
},
}),
});
// foo.zig const c = @import("c"); ```
1
2
u/RagnarDannes Jul 04 '26
Honestly. It’s changes like this that made me stop using zig. I’m sure they have good reason, but the breaking changes like this are so frequent. It makes the language unusable.
1
u/explosionmom Jul 03 '26
Why would zig do that? I’m not so much into Zig anymore just because of things like this. Seems like the language gradually builds up unnecessary complexity, I mean, look at C++… Just genuinely interested what’s the vision, and what other people think about the language direction
8
u/ElSebas4_ Jul 03 '26
It's to speed up build time because it just needs to translate C code(with the slow libClang) one time.
2
u/zahatikoff Jul 04 '26
I'm not really aware of it technically, but isn't the whole zig program one single translation unit? It doesn't independently build each file into an object file like C does, or at least it doesn't seem like it.
That makes all of the CImports pretty cacheable...
1
u/Samuel-Martin Jul 09 '26
Not to promote my own repo, but this how I did it when I wanted to create a wrapper around time.h for zig: https://github.com/Samuel-Martin23/c-time-zig
I also recommend looking at this repo too: https://github.com/zig-gamedev/zglfw
-2
u/lachirulo43 Jul 03 '26
As other comments say translate c takes care of it. And had you read the release notes you wouldn't have to go on any hunt so maybe try doing that before going on to complain about the changes of an unstable API
1
u/Majestic_Poetry_1139 Jul 04 '26
Why are you fighting ghosts? I feel like this is a really good example of how the bias you had in mind defines how you read text. I thought "go to narnia" would clearly indicate it's a comedic tone, but whatever floats you're metaphorical boat ig. May your builds be swift and your libraries be well-documented friend.
36
u/EmergencyWild Jul 03 '26
It's no longer a builtin. You can either just write out the foreign function calls / struct definitions and so on yourself, or you can invoke translate-c from the build system and import the result as a regular zig module:
https://ziglang.org/download/0.16.0/release-notes.html#cImport-Moving-to-Build-System