Below is how to setup the boiler plate code
mkdir test-addon
cd test-addon
git init
npm init
Now install the dependencies
npm install node-gyp --save-dev
npm install node-addon-api
node-gyp is the toolchain to compile the addons.
node-addon-api is a helper project as described earlier that will make writing C++ addons easier.
Couple of files that are important during the process are:
1. Bindings.gyp
2. package.json
Below is a sample content for this
Binding.gyp
{
"targets": [{
"target_name": "testaddon",
"cflags!": [ "-fno-exceptions" ],
"cflags_cc!": [ "-fno-exceptions" ],
"sources": [
"cppsrc/main.cpp"
],
'include_dirs': [
"<!@(node -p \"require('node-addon-api').include\")"
],
'libraries': [],
'dependencies': [
"<!(node -p \"require('node-addon-api').gyp\")"
],
'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ]
}]
}
package.json
{
"name": "test-addon",
"version": "1.0.0",
"description": "",
"main": "index.js",
"gypfile": true,
"scripts": {
"build": "node-gyp rebuild",
"clean": "node-gyp clean"
},
"author": "",
"license": "ISC",
"devDependencies": {
"node-gyp": "^3.7.0"
},
"dependencies": {
"node-addon-api": "^1.3.0"
}
}
Just to note, gypfile:true should be present here
Now having these files set up, compilation of the modules can be done in the below steps
npm run build
This will generate the testaddon.node file in the release folder
The add on can be used like this below
const testAddon = require('./build/Release/testaddon.node');
console.log('addon',testAddon);
module.exports = testAddon;
binding.gyp file contains all the files that need to be compiled and all the include files / libraries that the project will be using. If you notice we have added cppsrc/main.cpp file as our source file.
References:
https://medium.com/@atulanand94/beginners-guide-to-writing-nodejs-addons-using-c-and-n-api-node-addon-api-9b3b718a9a7f
mkdir test-addon
cd test-addon
git init
npm init
Now install the dependencies
npm install node-gyp --save-dev
npm install node-addon-api
node-gyp is the toolchain to compile the addons.
node-addon-api is a helper project as described earlier that will make writing C++ addons easier.
Couple of files that are important during the process are:
1. Bindings.gyp
2. package.json
Below is a sample content for this
Binding.gyp
{
"targets": [{
"target_name": "testaddon",
"cflags!": [ "-fno-exceptions" ],
"cflags_cc!": [ "-fno-exceptions" ],
"sources": [
"cppsrc/main.cpp"
],
'include_dirs': [
"<!@(node -p \"require('node-addon-api').include\")"
],
'libraries': [],
'dependencies': [
"<!(node -p \"require('node-addon-api').gyp\")"
],
'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ]
}]
}
package.json
{
"name": "test-addon",
"version": "1.0.0",
"description": "",
"main": "index.js",
"gypfile": true,
"scripts": {
"build": "node-gyp rebuild",
"clean": "node-gyp clean"
},
"author": "",
"license": "ISC",
"devDependencies": {
"node-gyp": "^3.7.0"
},
"dependencies": {
"node-addon-api": "^1.3.0"
}
}
Just to note, gypfile:true should be present here
Now having these files set up, compilation of the modules can be done in the below steps
npm run build
This will generate the testaddon.node file in the release folder
The add on can be used like this below
const testAddon = require('./build/Release/testaddon.node');
console.log('addon',testAddon);
module.exports = testAddon;
binding.gyp file contains all the files that need to be compiled and all the include files / libraries that the project will be using. If you notice we have added cppsrc/main.cpp file as our source file.
References:
https://medium.com/@atulanand94/beginners-guide-to-writing-nodejs-addons-using-c-and-n-api-node-addon-api-9b3b718a9a7f
No comments:
Post a Comment