moonbit-c-binding

Write MoonBit FFI bindings to C libraries with native stubs.

4|1|Updated Feb 15, 2026
One-click install
npx skills add https://github.com/Haoxincode/MoonBash --skill moonbit-c-binding
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: moonbit-c-binding
Source: https://github.com/Haoxincode/MoonBash/tree/main/.agents/skills/moonbit-c-binding
Command: npx skills add https://github.com/Haoxincode/MoonBash --skill moonbit-c-binding

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes scripts (resource) and references (resource) components.

What problem does it solve?

This Skill simplifies the complex process of integrating C libraries with MoonBit, enabling developers to leverage existing C codebases within their MoonBit projects without extensive manual FFI setup.

Core Features & Use Cases

  • FFI Declarations: Guides on writing extern "c" declarations for C functions.
  • C Stub Generation: Assists in creating necessary C stub files (moonbit.h).
  • Configuration: Helps configure moon.pkg or moon.pkg.json for native builds.
  • Memory Management: Provides guidance on ownership annotations (#borrow, #owned) and wrapping C pointers.
  • String Conversion: Details on converting strings between MoonBit and C.
  • Use Case: You need to use a C library for image processing in your MoonBit application. This Skill will guide you through setting up the native compilation, declaring the C functions, writing the C stubs, and handling memory management for the library's data structures.

Quick Start

Use the moonbit-c-binding skill to add extern "c" declarations for a C library.

Frequently Asked Questions about moonbit-c-binding

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

FAQPage Schema
How do I call C functions from MoonBit using native FFI?

To call C functions from MoonBit using native FFI, you write `extern "c"` declarations in MoonBit and create corresponding C stub files using `moonbit.h` to bridge the calls.

How do I configure moon.pkg for native compilation and C stubs?

Configuring `moon.pkg` for native compilation involves specifying `native-stub` and `link.native` settings to properly link C libraries and compile stubs for your MoonBit package.

How do I manage memory and C pointers in MoonBit FFI bindings?

Manage C pointers in MoonBit FFI by using ownership annotations like `#borrow` and `#owned`, and wrap raw C pointers with external objects and finalizers to ensure safe cleanup.

Can I use AddressSanitizer to validate MoonBit C bindings?

Yes, you can validate MoonBit C bindings with AddressSanitizer to detect memory leaks and unsafe pointer access across the FFI boundary during native compilation.

What is the best way to convert strings between MoonBit and C?

The best way to convert strings between MoonBit and C is to use the dedicated string conversion functions provided by the native FFI guidelines, ensuring safe data transfer across boundaries.

How do I implement C callbacks in MoonBit?

Implement C callbacks in MoonBit by setting up callback trampolines, which allow native C code to safely invoke MoonBit functions passed through the FFI interface.