|
| 1 | +# node-gyp |
| 2 | + |
| 3 | +C++ code needs to be compiled into executable form whether it be as an object |
| 4 | +file to linked with others, a shared library, or a standalone executable. |
| 5 | + |
| 6 | +The main reason for this is that we need to link to the Node.js dependencies and |
| 7 | +headers correcrtly, another reason is that we need a cross platform way to build |
| 8 | +C++ soucre into binary for the target platform, |
| 9 | + |
| 10 | +Until now **node-gyp** is the **de-fafto** standard build tool for writing |
| 11 | +Node.js addons. It's based on Google's **gyp** build tool, which abstract away |
| 12 | +many of the tedious issues related to cross platform building. |
| 13 | + |
| 14 | +**node-gyp** uses a file called ```binding.gyp``` that is located on the root of |
| 15 | +your addon project. |
| 16 | + |
| 17 | +```binding.gyp``` file, contains all building configurations organized with a |
| 18 | +JSON like syntax. The most important parameter is the **target** that must be |
| 19 | +set to the same value used on the initialization code of the addon as in the |
| 20 | +examples reported below: |
| 21 | + |
| 22 | +### **binding.gyp** |
| 23 | + |
| 24 | +```gyp |
| 25 | +{ |
| 26 | + "targets": [ |
| 27 | + { |
| 28 | + # myModule is the name of your native addon |
| 29 | + "target_name": "myModule", |
| 30 | + "sources": ["src/my_module.cc", ...], |
| 31 | + ... |
| 32 | + ] |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +### **my_module.cc** |
| 37 | + |
| 38 | +```cpp |
| 39 | +#include <napi.h> |
| 40 | + |
| 41 | +// ... |
| 42 | + |
| 43 | +/** |
| 44 | +* This code is our entry-point. We receive two arguments here, the first is the |
| 45 | +* environment that represent an independent instance of the JavaScript runtime, |
| 46 | +* the second is exports, the same as module.exports in a .js file. |
| 47 | +* You can either add properties to the exports object passed in or create your |
| 48 | +* own exports object. In either case you must return the object to be used as |
| 49 | +* the exports for the module when you return from the Init function. |
| 50 | +*/ |
| 51 | +Napi::Object Init(Napi::Env env, Napi::Object exports) { |
| 52 | + |
| 53 | + // ... |
| 54 | + |
| 55 | + return exports; |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | +* This code defines the entry-point for the Node addon, it tells Node where to go |
| 60 | +* once the library has been loaded into active memory. The first argument must |
| 61 | +* match the "target" in our *binding.gyp*. The second argument points to the |
| 62 | +* function to invoke. |
| 63 | +*/ |
| 64 | +NODE_API_MODULE(myModule, Init) |
| 65 | +``` |
| 66 | + |
| 67 | +## **node-gyp** reference |
| 68 | + |
| 69 | + - [Installation](https://www.npmjs.com/package/node-gyp#installation) |
| 70 | + - [How to use](https://www.npmjs.com/package/node-gyp#how-to-use) |
| 71 | + - [The binding.gyp file](https://www.npmjs.com/package/node-gyp#the-bindinggyp-file) |
| 72 | + - [Commands](https://www.npmjs.com/package/node-gyp#commands) |
| 73 | + - [Command options](https://www.npmjs.com/package/node-gyp#command-options) |
| 74 | + - [Configuration](https://www.npmjs.com/package/node-gyp#configuration) |
| 75 | + |
| 76 | +Sometimes finding the right settings for ```binding.gyp``` is not easy so to |
| 77 | +accomplish at most complicated task please refer to: |
| 78 | + |
| 79 | +- [GYP documentation](https://gyp.gsrc.io/index.md) |
| 80 | +- [node-gyp wiki](https://github.com/nodejs/node-gyp/wiki) |
0 commit comments