-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCPP Addons
More file actions
69 lines (66 loc) · 3.82 KB
/
Copy pathCPP Addons
File metadata and controls
69 lines (66 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
1.)Addons are native libraries that can be added as node module even though it is developed in other languages.
Eg: cpp libraries can be used direclty in node project as an addon with the help of special compilers and node packages.
2.)Modules used to achive the same are
- node-gyp => npm install --save-dev node-gyp. => dev dependency => This is to build the cpp source and convert to node module
- napi => npm install --save node-addon-api => dependency => Called N-API.
To build native addons from c/c++ source code which can be independent of the underlying JS engine such as V8 etc.
Napi methods and datatypes are used in cpp source code to create a wrapper for the actual cpp code
so that even if the underlying cpp library changes, it will impact less to the node part.
3.)Configuration file used: binding.gyp
Command to:
clean cpp src code: node-gyp clean
build cpp src code: node-gyp rebuild
Note: add both these as script in package.json and use npm run build/ clean
4.)Sample binding.gyp:
{
"targets": [{
"target_name": "test_addon", => It is mandatory to give target name. Else error comes on build
"sources": [
"cpp-src/main.cpp",
"cpp-src/sample.cpp"
],
'include_dirs':[ => A list of include directories to be passed on the command line to the C/C++ compiler (via -I or /I options).
"<!@(node -p \"require('node-addon-api').include\")"
],
'libraries': [],=> A list of list of libraries (and/or frameworks) on which this target depends.
'dependencies': [=> A list of targets on which this target depends.
Targets in other .gyp files are specified as ../path/to/other.gyp:target_we_want.
"<!@(node -p \"require('node-addon-api').gyp\")"
],
'defines': ['NAPI_DISABLE_CPP_EXCEPTIONS'] => commands to cpp compiler
}]
}
5.)What is .gyp?
Gyp is a documentation standard came along with Google chromium project which is the browser project on top of which
chrome is built and anybody can build their own custom browsers.
It's a cross platform build representation.
It helps to generate visual studio/ xcode/ scons or makefiles from build target file written in .gyp file
Syntax match wih Python script.
Mostly follow JSON format. # is comment.
Keywords:
'defines': A list of preprocesor definitions to be passed on the command line to the C/C++ compiler (via -D or /D options).
'libraries': List of libs to use in cpp
'dependencies': list of other target names on this or other gyp files on which this target building depends on
6.)binding.gyp kept in same hierarchy as of package.json can be used to build the cpp part into node addon using node-gyp rebuild
It will create build->Release->test_cpp_addon.node and obj.target etc. The name "test_cpp_addon".node comes from target name.
If we give special characters such as test-cpp-addon instead of underscore or valid characters, build will show error.
7.)Once build is done, this .node module which holds cpp equivalent code build independent of node version with the help of napi, can be used in .js files
as require('./build/Release/<targetname>.node') and use methods from it.
8.)How is cpp node binding done?
cpp code will be there in path specified as source in binding.gyp file.
For a single cpp file, there will be a wrapper as well. There will be a common main class too.
main.cpp:
---------
#include <napi.h>
#include "sample.h"
Napi::Object InitAll(Napi::Env env, Napi::Object exports){
return sample::Init(env, exports);
}
NODE_API_MODULE(test_cpp_addon, InitAll);
API: Sample.h:
--------------
#include <napi.h>
namespace sample{
int add(int a, int b);
Napi::Number addWrap(const Napi::CallbackInfo& );
}