Tangled for a long time, decided to write a little encounter "pit."
Basic Environment: Win7-64bit node (v7.5.0) These installations are simply too convenient to prepare themselves.
1. Install Python (2.7.x), install Node-gyp (3.5.0) with NPM, create a folder to store Binding.gyp hello.cc test.js
Binding.gyp content is as follows:
1 {2 "targets": [3 {4 "target_name": "Hello" , 5 " Sources ": [" hello.cc " ]6 }7 ]8 }
View Code
hello.cc content is as follows:
1#include <v8.h>2#include <node.h>3 4 using namespacenode;5 using namespaceV8;6 7 voidMethod (Constv8::functioncallbackinfo<v8::value>&args) {8v8::isolate* Isolate =args. Getisolate ();9 V8::handlescope scope (isolate);TenArgs. Getreturnvalue (). Set (V8::string::newfromutf8 (Isolate,"Hello World")); One } A - voidInit (v8::local<v8::object>target) { -Node_set_method (Target,"Hello", Method); the } - -Node_module (binding, init);
View Code
Test.js content is as follows:
var addon = require ('./build/release/hello '); Console.log (Addon.hello ());
View Code
2. Execute NODE-GYP Configure build directly
3. Try running it
Add:
Node-gyp Configure if you fail to try a few more times, after all, some site links are too slow
NODE-GYP build failure, compared to hello.cc (extra attention, hello.cc may not be compiled later, always pay attention to V8, timely modification)
Nodejs calling C + + first experience using addons (i)