Home > Web Front-end > JS Tutorial > body text

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

WBOY
Release: 2016-05-16 16:02:45
Original
1144 people have browsed it

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:

Copy code The code is as follows:

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

Write another 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 it runs correctly, a directory ----build will appear, under which the vs2013 project file will be generated for you, so that you can edit and compile in vs2013.
Of course, you can also directly use the command node-gyp build to compile.

The test js program is as follows:

Copy code The code is as follows:

var hello = require('./hello');
console.log(hello.hello());

Some problems were encountered, the records are as follows:

1. C:UsersAdministrator.node-gyp

2. The hello in NODE_MODULE(hello, init) is the module name and needs to be consistent with the file name. Otherwise, there will be no problem in compilation but an error will occur during runtime. Because when require('./hello.node'), it not only finds the corresponding file, but also matches the corresponding module.

3. I followed Pu Ling's book "Node.js in a Simple Language" and referred to some web pages to study. There is a conditions item in the gyp project file given in the book, 'libraries': ['- lnode.lib'], because of this sentence, I kept getting an error when compiling: can't open node.lib. Obviously the file existed, but it just reported an error. I searched a lot of information but couldn't solve it. Then I copied node.lib directly. Go to the working directory and use the command line to compile successfully! But in vs2013, the error was still the same. Nothing I thought was right. Finally, I went to the official website and found that none of the examples provided this parameter. So I tried to delete it, and everything was OK! Dear gods, who can give a correct explanation? !

The above is the entire content of this article, I hope you all like it.

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template