Home php教程 PHP开发 Use C++ to write extension modules for node.js

Use C++ to write extension modules for node.js

Dec 26, 2016 am 09:42 AM

Prerequisite: Install node.js, Python2.7 and visual studio 2013.

Process:
First install the GYP project generation tool, npm install -g node-gyp.
Create the test directory, which is our working directory. Create a src directory under this directory to store the C++ source code. Create a new text file named binding.gyp. This is the gyp project file. The content is as follows:

{
    'targets':[{
        'target_name':'hello',
        'sources':['src/hello.cc']
    }]
}
Copy after login

Write a simple hello.cc with the following content:

#include <node.h>
using namespace v8;
 
Handle<Value> Hello(const Arguments& args) {
 HandleScope scope;
 return scope.Close(String::New("Hello world!"));
}
 
void init(Handle<Object> target) {
 NODE_SET_METHOD(target, "hello", Hello);
}
 
NODE_MODULE(hello, init)
Copy after login

Then run the command: node-gyp configure
If run correctly, a directory ----build will appear, under which vs2013 is generated for you Project file, so that it can be edited and compiled in vs2013.
Of course, you can also directly use the command node-gyp build to compile.

The test js program is as follows:

var hello = require(&#39;./hello&#39;);
console.log(hello.hello());
Copy after login

Some problems were encountered and the records are as follows:

1. C:UsersAdministrator.node-gyp

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)