Nim c bindings

From planetis-m/skills · View interactive version

Nim c bindings is an AI agent skill from the GitHub repository planetis-m/skills. Bind C libraries to Nim and make them build reliably across Linux, macOS, and Windows, including headers, types, linking, shared-library loading, and CI/release workflows. Use when wrapping a C dependency, fixing Nim FFI build issues, or setting up cross-platform GitHub Actions for a Nim native libr

How to use this skill

Skill file

Nim C Bindings & CI

Rules

Binding Fundamentals

clong, culong, clonglong, csize_t, cfloat, cdouble, cstring,

and pointer types.

value.

Put ergonomic aliases and conversions in a separate wrapper layer.

Linking

header supplies declarations for generated C; a runtime-only dynlib proc

should not also use header.

Runtime Library Resolution

Platform Rules

| Platform | Toolchain | Deps | Link flags | Runtime |

|----------|-----------|------|------------|---------|

| Linux | System GCC/Clang | apt | -l<name> or -L<dir> -l<name> | Colocate local .so; rpath $ORIGIN if needed |

| macOS | Apple Clang | Homebrew | staticExec("brew --prefix") for -I/-L | Colocate local .dylib |

| Windows | MSVC (--cc:vcc) | vcpkg only | .lib/.dll.lib full paths | Colocate .dll next to .exe |

CI & Release

Verification

with C sizeof and offsetof probes when layout is in doubt.

a clean directory.

Workflow

  1. Identify the C API. Determine calling convention, opaque vs value types, and ownership.
  2. Write bindings. Use importc, correct calling convention, incompleteStruct for opaque types, bycopy for value types. Add header pragma.
  3. Set up linking. System libs: -l only. Local libs: -L + -l with repo-relative paths. Windows: explicit .lib paths.
  4. Handle runtime. Colocate local shared libs. For system-installed libs, rely on the normal loader search path. Add rpath $ORIGIN on Linux only for colocated local libs.
  5. Verify the ABI. Run a real C round trip for the types and calls used by

the binding.

  1. Add CI. Copy references/ci.yml, adapt placeholders (<package>, <src/main.nim>, dependency lists).
  2. Add release. Copy references/release.yml, adapt placeholders, configure draft releases.
  3. Test locally, then push. Verify the build works locally with the same flags CI uses.

Common Mistakes

| Mistake | Why it's wrong |

|---------|----------------|

| Hardcoding -L paths for system libraries | OS toolchain already knows where they are; breaks portability |

| Mixing MSVC + MSYS2/MinGW on Windows | Incompatible ABIs, linker errors, runtime crashes |

| Using implicit -l<name> with MSVC | MSVC doesn't resolve libs the same way GCC does; use explicit .lib paths |

| Relying on env vars for vendored shared libs | Fragile across machines; colocate instead |

| Build-tree-only rpaths | Breaks when the binary moves; use $ORIGIN for colocated libs |

| Guessing Windows dependency paths | Non-deterministic; use vcpkg with known install roots |

| Compiling C sources into the executable when the task requires an existing shared library | Bypasses shared-library link and relocation behavior |

| Adding Nim-bodied wrappers to a requested raw binding surface | Hides whether the C API was imported directly and changes the requested layer |

References

Atlas, and vcpkg.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.