cangjie-cffi

Declare and invoke C functions from Cangjie with CFunc and LibC memory management.

4|1|Updated Mar 29, 2026
One-click install
npx skills add https://github.com/JunjieChen0/NJUST --skill cangjie-cffi
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: cangjie-cffi
Source: https://github.com/JunjieChen0/NJUST/tree/main/.njust-ai/skills/cangjie-cffi
Command: npx skills add https://github.com/JunjieChen0/NJUST --skill cangjie-cffi

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill enables robust cross-language interoperation between Cangjie and C/C++ code. It standardizes how to declare external C functions, map C structs, pass inout pointers, manage memory via LibC, and construct safe callbacks using CFunc and CString, reducing boilerplate and risk.

Core Features & Use Cases

  • Foreign declarations: declare external C functions from Cangjie with optional @C qualifiers, and call them inside unsafe blocks.
  • CFunc and inout: define function pointers and automatically pass addresses for inout parameters, enabling bidirectional callbacks.
  • Memory management: allocate and free native memory via LibC, work with CPointer<T>, CString, and resource management utilities.
  • Interop workflows: Create and manipulate C pointers, maps between C types and Cangjie types, and implement C callbacks from Cangjie side.
  • Use Case: Build a plugin or extension that integrates with an existing C API, handling strings and structured data safely from Cangjie.

Quick Start

Use the cangjie-cffi skill to declare a foreign function and call it from Cangjie inside an unsafe block.

Frequently Asked Questions about cangjie-cffi

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

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

You can declare external C functions in Cangjie using foreign declarations with optional @C qualifiers, then invoke them inside unsafe blocks. This standardizes cross-language calls and reduces boilerplate when integrating existing C APIs.

How does memory management work when passing C pointers in Cangjie?

Memory management for C pointers in Cangjie involves allocating and freeing native memory via LibC. You use CPointer<T> and CString types to safely handle native resources and structured data across the language boundary.

Can I create C callbacks from the Cangjie side using CFunc?

Yes, you can define function pointers with CFunc and pass them as callbacks to C code. This mechanism supports bidirectional communication, allowing C libraries to invoke Cangjie functions through standardized foreign function interfaces.

How do I pass inout parameters to C functions in Cangjie?

You pass inout parameters to C functions by automatically passing their addresses to the native code. This allows bidirectional data flow, enabling C functions to modify Cangjie variables directly through pointer references.

Do I need unsafe blocks for foreign function interop in Cangjie?

Yes, unsafe blocks are required when invoking foreign C functions in Cangjie. Because C code operates outside Cangjie's safety guarantees, unsafe blocks explicitly mark the boundary where memory and pointer operations need manual validation.

What is the best way to map C structs to Cangjie types?

The best way to map C structs is using the @C qualifier on struct definitions with CType constraints. This ensures the memory layout matches the C ABI, allowing safe passage of structured data between Cangjie and C code.