Questions tagged [zig]

Zig is an open-source programming language designed for robustness, optimality, and clarity.

Zig is a general-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.

Website:

Documentation:

Zig has many features for low-level programming, notably: packed structs (structs without padding between fields), arbitrary width integers and multiple pointer types. Zig has no hidden control flow, no hidden memory alocation, no macros (but therefor compile time code execution)

212 questions
1
vote
1 answer

Idiomatic alternative to C's `system` function in Zig

Is there a way to make a system call in Zig, other than using a cImport? right now I've got it to work with something like that: const c = @cImport({ @cInclude("stdlib.h"); }); // ... const result: c_int = c.system(cmd); While this works…
1
vote
1 answer

Valgrind illegal hardware instruction with Zig

I'm trying to debug the memory using Valgrind for a simple Zig code that leaks memory. This is the code I'm using const std = @import("std"); const Point = struct { x: i32, y: i32, }; pub fn main() !void { const allocator =…
Isky
  • 1,328
  • 2
  • 14
  • 33
1
vote
1 answer

How to do generics in a struct field in zig

I'm very new to zig and I'm wondering how to create a struct field that can have a compile time known type for example something similar to the comptime keyword when used with a function parameter, I want to do the same thing to a struct field that…
Mahmoud Hany
  • 139
  • 1
  • 11
1
vote
1 answer

How do I specifiy the "-rdynamic" compiler/linker option in a zig.build file?

I'm using zig 0.10.1 and I'm trying to build a wasm library. Most tutorials show how to do this with zig build-lib. For a wasm library to export symbols one needs to specify the -rdynamic option. Now that I'm trying to build the library with a…
Gregor Budweiser
  • 218
  • 3
  • 10
1
vote
2 answers

How to change executed method in runtime using function pointers?

I want to use a function pointer as a struct field to have a runtime choice of an executed method. For example, I want this code to print 010: const std = @import("std"); fn Foo() type { return struct { const Self = @This(); …
KindFrog
  • 358
  • 4
  • 17
1
vote
1 answer

How to print a number as hexadecimal in Zig?

const std = @import("std"); pub fn main() void { const foo: u8 = 128; std.debug.print("{}\n", .{foo}); // "128" } The above prints 128 as expected. How can the value be printed as hexadecimal instead?
Costava
  • 175
  • 9
1
vote
1 answer

expected type 'i32', found 'usize'

I'm currently trying to solve some leetcode problems in Zig, and I'm doing the two sum problem. Here's what the entirety of my code looks like: const std = @import("std"); const allocator = std.heap.page_allocator; fn two_sum(nums: []i32, target:…
1
vote
1 answer

Convert array to a tuple

I have a function that accepts a struct fn foo(args: anytype) void { const ArgsType = TypeOf(args); const args_type_info = typeInfo(ArgsType); switch (args_type_info) { .Struct => { // do work }, else =>…
KindFrog
  • 358
  • 4
  • 17
1
vote
2 answers

How to change the local cache directory for the zig build system?

Motivation: my source files are on a network (samba) share. zig build fails with error: Unexpected . To overcome it (otherwise I have to delete the ./zig-cache/ folder before every build) and to save time (and potentially the SSD drive) I want to…
MKaama
  • 1,732
  • 2
  • 19
  • 28
1
vote
1 answer

use c's `nmmintrin.h` in zig

I'm trying to work through this C coding exercise, but I want to do so in Zig. In the exercise they #import (@~18:15) to use __builtin_popcount. From a brief search it looks like this is a gcc-specific header file? Not sure what…
CrepeGoat
  • 2,315
  • 20
  • 24
1
vote
1 answer

Is there something similar to perror?

I've been test running zig recently, and have been able to pick up most of it by simply reading the standard library. However, I can't find anything similar to how we do perror/strerror in C. Is there a way to get a string representation for…
Jason
  • 2,493
  • 2
  • 27
  • 27
1
vote
1 answer

How can I integrate cimgui with GLFW and Vulkan into a zig project?

I'm trying to use GLFW with Vulkan and cimgui. GLFW and Vulkan and being included and linked fine (there were no issues before I included cimgui). I include all of the C into my project like so: const c = @cImport({ // C Imgui …
NaniNoni
  • 155
  • 11
1
vote
1 answer

Creating a 32-bit Windows DLL using Zig without the C runtime and removing unnecessary exports

I am new to the Zig language and have a question about creating a 32-bit Windows DLL. I am using zig-windows-x86_64-0.11.0-dev.2612+56d800ff7.zip. I have created a project using zig init-lib, but the generated build.zig seemed to be for a static…
noob
  • 11
  • 2
1
vote
1 answer

How to `#define` in build.zig for an imported C library

Based on my research *LibExeObjStep's defineCMacro allows me to #define just like I would in C but it's not getting included in the package output: --pkg-begin raygui C:\git\raylib-zig-experiments\lib\raygui-zig.zig --pkg-end -D…
Jordan
  • 3,813
  • 4
  • 24
  • 33
1
vote
0 answers

How to get the address of a label using inline assembly on Windows?

I am trying to get the address of a label using inline asm in Zig. const main_ccb_p1 = core.mainCCCBPtr(); comptime var to_return_label = ".to_return" ++ mangle(function); asm volatile ( \\ # save main context \\ movq %rbx, 0(%[ctx]) #…