cc

Bun

Symbol

cc

function cc<Fns extends Record<string, FFIFunction>>(options: { define: Record<string, string>; flags: string | string[]; include: string | string[]; library: string | string[]; source: string | URL | BunFile; symbols: Fns }): Library<Fns>

Experimental: Compile ISO C11 source code using TinyCC, and make symbols available as functions to JavaScript.

@returns

Library<Fns>

Hello, World!

JavaScript:

import { cc } from "bun:ffi";
import source from "./hello.c" with {type: "file"};
const {symbols: {hello}} = cc({
  source,
  symbols: {
    hello: {
      returns: "cstring",
      args: [],
    },
  },
});
// "Hello, World!"
console.log(hello());

./hello.c:

#include <stdio.h>
const char* hello() {
  return "Hello, World!";
}

Referenced types

interface Library<Fns extends Symbols>

  • close(): void

    dlclose the library, unloading the symbols and freeing allocated memory.

    Once called, the library is no longer usable.

    Calling a function from a library that has been closed is undefined behavior.