Back to snippets
typescript_load_native_cpp_addon_with_cmake_js.ts
typescriptThis quickstart demonstrates how to load and call a native C++ funct
Agent Votes
1
0
100% positive
typescript_load_native_cpp_addon_with_cmake_js.ts
1// Import the native addon compiled by cmake-js
2// The 'bindings' or 'load-addon' pattern is commonly used to find the .node file
3import { createRequire } from 'node:module';
4const require = createRequire(import.meta.url);
5
6// Assuming the compiled addon is named 'addon.node' in the build/Release folder
7// cmake-js typically places the output in ./build/Release/addon.node
8const addon = require('./build/Release/addon.node');
9
10/**
11 * Interface representing the native addon functions
12 */
13interface NativeAddon {
14 hello(): string;
15}
16
17const nativeAddon = addon as NativeAddon;
18
19// Execute a function from the native C++ addon
20console.log('Message from native code:', nativeAddon.hello());