Nodejs calling C + + first experience using addons (i)

Source: Internet
Author: User
Tags install node

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.