Skip to content

Introduction

cjbind can automatically generate Cangjie FFI bindings for C.

For example, given the following C header file example.h:

c
#ifndef CANGJIE_H
#define CANGJIE_H

typedef enum {
    CAT,
    DOG,
    BIRD
} AnimalType;

typedef struct {
    AnimalType type;
    int age;
    char name[32];
} Animal;

void print_animal(const Animal* animal);

int compute_sum(int a, int b);

#endif

After using cjbind, it will generate Cangjie FFI binding code, allowing you to call functions from C libraries and use their types:

cangjie
// Generated by cjbind 0.1.0, DO NOT EDIT

package cjbind_ffi

public const AnimalType_CAT: AnimalType = 0
public const AnimalType_DOG: AnimalType = 1
public const AnimalType_BIRD: AnimalType = 2

public type AnimalType = UInt32

@C
public struct Animal {
    public let type_: AnimalType
    public let age: Int32
    public let name: VArray<Int8, $32>

    init(type_: AnimalType, age: Int32, name: VArray<Int8, $32>) {
        this.type_ = type_
        this.age = age
        this.name = name
    }
}

foreign func print_animal(animal: CPointer<Animal>): Unit

foreign func compute_sum(a: Int32, b: Int32): Int32

Released under the MIT License