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

Source: Internet
Author: User
Tags install node

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

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

Process:
Install the GYP project generation tool npm install-g node-gyp.
Create the test directory, which is our working directory. Create another src directory to store the C ++ source code and create a new one named binding. gyp text file, which is a gyp project file with the following content:

Copy codeThe Code is as follows:
{
'Targets ':[{
'Target _ name': 'hello ',
'Sources': ['src/hello. CC']
}]
}

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)

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

The test js program is as follows:

Copy codeThe Code is as follows:
Var hello = require ('./hello ');
Console. log (hello. hello ());

Some problems were encountered, as shown below:

1. C: \ Users \ Administrator. node-gyp \ 0.10.33 directory does not have the default Debug directory. When compiled into the debug file in vs2013, the error LNK1104: cannot open file 'C: \ Users \ Administrator is displayed. node-gyp \ 0.10.33 \ Debug \ node. lib, create a Debug directory, and put the node under the same directory as your operating system environment. copy lib to it.

2. hello in NODE_MODULE (hello, init) is the module name, which must be consistent with the file name. Otherwise, compilation will be normal and an error will occur during running. Because when require ('./hello. node'), both find the corresponding file and match the corresponding MODULE.

3. I correspond to the simple introduction of node. js, which is based on some web pages for learning. The gyp project file provided by the book contains a conditions item, 'libraries': ['-lnode. lib '], because of this sentence, the error always occurs during compilation: can't open node. lib, clearly the file exists, but it is an error. I found a lot of information and did not solve it. Then I put node. lib is directly copied to the working directory and compiled with the command line! However, in vs2013, the error still persists. I thought it was wrong. I finally went to the official website and found that no such parameter was provided in other examples. Then I tried to delete the parameter, all results are OK! Who can give a correct explanation ?!

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

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.