Skip to content

aiskillstore/marketplace / skills/0xbigboss/zig-best-practices

zig-best-practices

Verified

Use when reading or writing Zig files (.zig, build.zig, build.zig.zon).

FileKindSize
C-INTEROP.mdmarkdown2.0 KB
DEBUGGING.mdmarkdown1.6 KB
GENERICS.mdmarkdown1.7 KB
SKILL.mdskill md3.3 KB
skill-report.jsonconfig31.9 KB

C-INTEROP.md

View on GitHub
# C Interoperability in Zig

Zig can directly import C headers, call C functions, and expose Zig functions to C. Use these patterns when integrating with existing C libraries or system APIs.

## When to Use

- Wrapping C libraries (raylib, SDL, curl)
- Calling platform-specific system APIs
- Passing callbacks to C code
- Writing Zig libraries callable from C

## Importing C Headers

Use `@cImport` to import C headers directly:

```zig
const ray = @cImport({
    @cInclude("raylib.h");
});

pub fn main() void {
    ray.InitWindow(800, 450, "window title");
    defer ray.CloseWindow();

    ray.SetTargetFPS(60);
    while (!ray.WindowShouldClose()) {
        ray.BeginDrawing();
        defer ray.EndDrawing();
        ray.ClearBackground(ray.RAYWHITE);
    }
}
```

Configure include paths in `build.zig`:

```zig
exe.addIncludePath(.{ .cwd_relative = "/usr/local/include" });
exe.linkSystemLibrary("raylib");
```

## Extern Functions (System APIs)

Call platform APIs without bindings using `extern`:

```zig
const win = @import("std").os.windows;

extern "user32" fn MessageBoxA(
    ?win.HWND,
    [*:0]const u8,
    [*:0]const u8,
    u32,
) callconv(.winapi) i32;
```

## C Callbacks

Pass Zig functions to C libraries using `callconv(.C)`:

```zig
fn writeCallback(
    data: *anyopaque,
    size: c_uint,
    nmemb: c_uint,
    user_data: *anyopaque,
) callconv(.C) c_uint {
    const buffer: *std.ArrayList(u8) = @alignCast(@ptrCast(user_data));
    const typed_data: [*]u8 = @ptrCast(data);
    buffer.appendSlice(typed_data[0 .. nmemb * size]) catch return 0;
    return nmemb * size;
}
```

Key points:
- `callconv(.C)` makes the function callable from C
- `*anyopaque` is Zig's equivalent of `void*`
- Use `@alignCast` and `@ptrCast` to recover typed pointers
- Return 0 on error (C convention) since Zig errors can't cross FFI boundary

## C Types Mapping

| C Type | Zig Type |
|--------|----------|
| `void*` | `*anyopaque` |
| `char*` | `[*:0]const u8` (null-terminated) |
| `size_t` | `usize` |
| `int` | `c_int` |
| `unsigned int` | `c_uint` |
| `NULL` | `null` |
slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack."Stars: 198kUpdated Claude CodeCodexVerifiedInstall
ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read.Stars: 194kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report.Stars: 194kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt.Stars: 194kUpdated Claude CodeCodexVerifiedInstall