The method of using C + + to write extension modules for node.js under WINDOW7

Source: Internet
Author: User
Tags require install node

The method of using C + + to write extension modules for node.js under Window7

This article introduces the use of C + + in Window7 under the node.js to write extension module method, with a simple hello example, the need for small partners can refer to.

Prerequisite: Install Node.js, Python2.7 and Visual Studio 2013.

Process:

First install the GYP Project Generation tool, NPM install-g Node-gyp.

Set up the test directory, this is our working directory, in this directory to build a src directory, used to store C + + source code, another new text file called Binding.gyp, this is the GYP project file, the contents are as follows:

The code is as follows:

{

' Targets ': [{

' target_name ': ' Hello ',

' Sources ': [' src/hello.cc ']

}]

}

Write a simple hello.cc that reads as follows:

?

1 2 3 4 5 6 7 8 9 10 11 12-13 #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 works correctly, a directory----build is created, and the vs2013 project file is generated for you so that you can edit and compile it in vs2013.

You can, of course, compile directly with the command NODE-GYP build.

The test JS program is as follows:

The code is as follows:

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

Console.log (Hello.hello ());

A number of problems have been encountered, as recorded below:

1, c:usersadministrator.node-gyp.10.33 This directory, there is no default debug directory, in vs2013 compiled into the debug file will be prompted error lnk1104:cannot open file ' C: Usersadministrator.node-gyp.10.33debugnode.lib ', create a debug directory and copy the Node.lib in the same directory as your operating system environment.

2, Node_module (Hello, init) in the Hello is the module name, need to be consistent with the file name, otherwise compile no problem, run error. Because when require ('./hello.node '), both find the corresponding file, also match the corresponding module.

3, I correspond to the Pauling of "simple Node.js" this book, and refer to some Web pages for learning, the book gives the GYP project document has a conditions, ' libraries ': ['-lnode.lib '], because of this sentence, Compile always error: can ' t open node.lib, obviously the file is there, but it is an error, looking for a lot of information, also did not solve, and then I put the node.lib directly to the working directory, with the command line compiled successfully! But in the vs2013, the mistake is still, I don't think it's right , the last officer net, found that the example of others did not give such parameters, I tried to delete this dongdong, the result of all ok! great God, who can give a correct explanation?!

The above mentioned is the entire content of this article, I hope you can enjoy.

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.