Skip to main content
dlopen loads a shared library and returns a Library instance with typed symbols.

Syntax

function dlopen<Fns extends Record<string, FFIFunction>>(
  libName: string,
  defs: Fns,
): Library<Fns>;

Example

import { dlopen, FFIType } from 'arkffi';

const lib = dlopen('libffi_target.so', {
  add:     { args: [FFIType.double, FFIType.double],     returns: FFIType.double },
  compute: { args: [FFIType.int32, FFIType.double, FFIType.CString], returns: FFIType.double },
});

lib.symbols.add(2.0, 3.0);
lib.symbols.compute(0, 4.0, 'square');
lib.close();

Parameters

ParameterTypeDescription
libNamestringPath or name of the .so library
defsRecord<string, FFIFunction>Map of function names to type definitions

FFIFunction

interface FFIFunction {
  args: string[];        // Parameter type codes
  returns: string;       // Return type code
  threadsafe?: boolean;  // For JSCallback definitions
}

Type Inference

dlopen uses TypeScript generics to infer the shape of symbols, providing IDE autocompletion for all defined function names.
const lib = dlopen('lib.so', {
  myFunc: { args: [FFIType.double, FFIType.double], returns: FFIType.double },
});
lib.symbols.myFunc(2.0, 3.0); // autocompleted

Return Value

Returns a Library instance with typed symbols.