Nim C Interop

Enable Nim-C interop with importc/exportc pragmas and typed boundaries.

187|20|Updated Nov 20, 2025
One-click install
npx skills add https://github.com/TheBushidoCollective/han --skill nim-c-interop
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: Nim C Interop
Source: https://github.com/TheBushidoCollective/han/tree/main/jutsu/jutsu-nim/skills/nim-c-interop
Command: npx skills add https://github.com/TheBushidoCollective/han --skill nim-c-interop

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Enable Nim programs to call C libraries and expose Nim code to C applications, with safe, typed boundaries.

Core Features & Use Cases

  • Importing C functions via pragmas (importc/exportc)
  • Wrapping C libraries with Nim wrappers
  • Header generation and type-safe interop patterns

Quick Start

Create a Nim wrapper for a C function using importc pragma and call it from Nim.

Frequently Asked Questions about Nim C Interop

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I call C functions from Nim code?

Use the importc pragma to declare C functions in Nim. The pragma tells Nim to link to the C function by name, enabling direct calls with type-safe bindings. This creates zero-overhead interop between Nim and C libraries.

Can I expose Nim functions to C applications?

Yes, use the exportc pragma to mark Nim procedures for C interop. Exportc generates C-compatible function signatures that C code can call directly, maintaining ABI compatibility across language boundaries.

What's the best way to wrap a C library in Nim?

Create Nim wrapper types and procedures that map C types to Nim equivalents using importc pragmas. Define type-safe interfaces that hide C implementation details, then call wrapped C functions through the Nim layer for safer, more idiomatic code.

How do I handle memory layout and struct compatibility between Nim and C?

Use pragmas and type annotations to control memory layout, ensuring Nim struct definitions match C struct ABI. Attributes like packed and alignment guarantee field offsets align, preventing memory corruption when passing structs across the FFI boundary.

Do I need special handling for callbacks across Nim and C?

Yes, callbacks require careful FFI patterns. Define callback types compatible with C function pointers, use proper calling conventions, and manage lifetime to prevent dangling pointers when C code invokes Nim callbacks.