A minimal example of calling C++ functions from Bun using FFI.
cpp/cpp_math.cpp
- C++ source with a simpleadd()
functionbuild_cpp.sh
- Compiles C++ to a shared librarybun/index.js
- Bun script that calls the C++ function via FFIbuild_bun.sh
- Compiles the Bun script into a standalone executable, including the C++ shared library
- Build the C++ library:
./build_cpp.sh
- Run the Bun script:
bun run bun/index.js
- Build the Bun executable:
./build_bun.sh
Run the Bun executable:
./dist/bun_binary
Expected output:
C++ Add: 10 + 20 = 30
C++ Multiply: 10 * 20 = 200
JS Add: 10 + 20 = 30
JS Multiply: 10 * 20 = 200