moonbit-c-binding

Write MoonBit FFI bindings to C libraries with stubs and configuration.

5|1|Updated Mar 2, 2026
One-click install
npx skills add https://github.com/golemcloud/moonbit-sdk --skill moonbit-c-binding-golemcloud
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: moonbit-c-binding
Source: https://github.com/golemcloud/moonbit-sdk/tree/main/.agents/skills/moonbit-c-binding
Command: npx skills add https://github.com/golemcloud/moonbit-sdk --skill moonbit-c-binding-golemcloud

SYSTEM DOCUMENTATION & REQUIREMENTS

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

What problem does it solve?

This Skill provides a comprehensive guide and workflow for integrating C libraries into MoonBit projects using native Foreign Function Interface (FFI) capabilities, simplifying complex interoperability tasks.

Core Features & Use Cases

  • FFI Declarations: Write extern "c" declarations for C functions.
  • C Stub Generation: Create necessary C stub files (moonbit.h).
  • Build Configuration: Configure moon.pkg or moon.pkg.json for native builds.
  • Memory Management: Understand and apply #borrow/#owned annotations and external object patterns for safe memory handling.
  • String Conversion: Handle string encoding differences between MoonBit (UTF-16) and C (UTF-8).
  • Testing & Validation: Utilize AddressSanitizer for robust memory bug detection.

Quick Start

Use the moonbit-c-binding skill to generate the necessary FFI declarations and C stubs for integrating the 'libexample' C library into your MoonBit project.

Frequently Asked Questions about moonbit-c-binding

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

FAQPage Schema
How do I bind a C library to MoonBit using native FFI?

To bind a C library to MoonBit, you write `extern "c"` declarations for C functions and create C stub files using `moonbit.h`. You then configure `native-stub` and `link.native` in your `moon.pkg` to link the native build.

How does memory management work across the MoonBit and C FFI boundary?

Memory management across the FFI boundary uses `#borrow` and `#owned` ownership annotations. You wrap C pointers with external objects and finalizers to ensure safe memory handling and prevent leaks.

How do I handle string conversion between MoonBit and C?

You handle string conversion by managing encoding differences between MoonBit's UTF-16 strings and C's UTF-8 strings. The binding process requires explicit conversion logic to safely pass strings across the FFI boundary.

Can I use AddressSanitizer to validate MoonBit C bindings?

Yes, you can use AddressSanitizer to validate MoonBit C bindings. It enables robust memory bug detection during native integration, ensuring your external object finalizers and pointer management work correctly.

How do I implement C callbacks in MoonBit?

You implement C callbacks in MoonBit using callback trampolines. These trampolines bridge C function calls back into MoonBit code safely while maintaining proper context and memory ownership.

What do I need to configure in moon.pkg to build native C stubs?

You need to configure `native-stub` and `link.native` settings in your `moon.pkg` or `moon.pkg.json` file. This configuration directs the MoonBit build system to compile and link your C stubs correctly.