Develop node. js C/C ++ native extensions in Windows

Source: Internet
Author: User
Tags visual studio 2010
Preparations

(1)Local System Description: my machine is win7 64-bit, 32-bit can also be.

(2)Software installation:

Visual c ++ 2010 EXPRESS (Visual Studio 2010 is also supported );

Install NodeJS under windows, you can download the msi version from the official website http://www.nodejs.org for quick installation;

(3)Source code preparation: Download Source code=node-v0.10.5.tar from the official website (the author download the latest code version v0.10.5), decompress to any windows Directory, such as D: \ node-v0.10.5.

Write node C/C ++ native extensions

[1]Open Windows Command Line cmd.exe, enter D: \ node-v0.10.5, execute vcbuild. bat release, and finally in D: \ node-v0.10.5 \ releasedirectory can find compiled node.exe, node. lib and other files.

[2]Create, compile, and install a batch file named "nodins. bat". The content of the file is as follows:

@echo offif "%1"=="" goto helpmkdir "%1"mkdir "%1"\includecopy /y src\node.h "%1"\includecopy /y src\node_object_wrap.h "%1"\includecopy /y src\node_buffer.h "%1"\includecopy /y src\node_version.h "%1"\includecopy /y deps\v8\include\*.h "%1"\include\copy /y deps\uv\include\*.h "%1"\include\mkdir "%1"\include\uv-privatecopy /y deps\uv\include\uv-private\*.h "%1"\include\uv-privatemkdir "%1"\include\evcopy /y deps\uv\src\ev\*.h "%1"\include\evmkdir "%1"\include\c-arescopy /y deps\uv\include\ares.h "%1"\include\c-arescopy /y deps\uv\include\ares_version.h "%1"\include\c-aresmkdir "%1"\libcopy /y Release\node.lib "%1"\libcopy /y Release\node.exe "%1"echo =================================echo Install succeefully!goto exitif not errorlevel 0 echo Error "install-path" & goto exit:helpecho nodins.bat install-path:exit

[3]Open Windows Command Line cmd.exe, switch to D: \ node-v0.10.5 directory. Set the file nodins. bat copies to D: \ node-v0.10.5 and runs: nodins on the command line. bat D: \ nodejs, generate the compilation C/C ++ extended compilation environment (including header files, libraries, and executable files) D: \ nodejs directory, the content is as follows:

 

[4]Use Visual Studio 2010 to create a DLL project blank project hellonode under the D: \ directory, create a project-> win32 console program, and then enter the following page:

 

[5]Create a C ++ file, such as hellonode. cpp. The Code is as follows:

#define BUILDING_NODE_EXTENSION#include <node.h>using namespace v8;Handle<Value> Hello(const Arguments& args) {  HandleScope scope;  return scope.Close(String::New("Hello world!"));}Handle<Value> Add(const Arguments& args) {  HandleScope scope;  if (args.Length() < 2) {    ThrowException(Exception::TypeError(String::New("Wrong number of arguments")));    return scope.Close(Undefined());  }  if (!args[0]->IsNumber() || !args[1]->IsNumber()) {    ThrowException(Exception::TypeError(String::New("Wrong arguments")));    return scope.Close(Undefined());  }  Local<Number> num = Number::New(args[0]->NumberValue() +      args[1]->NumberValue());  return scope.Close(num);}void init(Handle<Object> target) {  NODE_SET_METHOD(target, "hello", Hello);  NODE_SET_METHOD(target, "add", Add);}NODE_MODULE(hellonode, init)

[6]Change the output directory to. \ in the configuration Properties Section of project properties .\;

[7]Change the extension of the target file to. node in the configuration Properties Section of project properties;

[8]Add the header file directory to "D: \ nodejs \ include" in the "C/C ++-General-Add the include directory" configuration property of the project property.

[9]Add the Directory D: \ nodejs \ lib to the project property configuration property-linker-General-additional library directory

[10]Add the lib Library: node. lib to the configuration properties-linker-input-Additional dependency

[11]After compilation, a file hellonode. node is generated in D: \ hellonode;

[12]Create the js test code test. js in the D: \ hellonode directory. The Code is as follows:

var addons = require('./hellonode');console.log('C/C++ addons.hello() =', addons.hello());console.log('C/C++ addons.add(200, 300) =', addons.add(200, 300));

[13]Run node. \ test.js(if node.exe PATH variable is not configured, run: D: \ nodejs \ node. \ test. js) on the command line:

 

Compilation and generation problems

[1] fatal error LNK1112: the module computer type "X86" conflicts with the target computer type "x64"

Cause: For a 64-bit System in win7, the DLL required for running the program must be compiled by a 64-bit system, and VS2010 must also be installed, supports 32-bit compilation. If you have already selected this option during installation, the solution to this problem is as follows:

(1) Right-click the project name and click Properties. The project properties page is displayed. Find the linker ---- advanced, modify the target computer on the right, and select the option with X64.

(2) Right-click the project name, select the cleanup solution, select the X64 platform compiler, and then regenerate the solution to debug the program successfully. Select the X64 platform compiler, for example:

If x64 cannot be found in the drop-down list of the selected platform, it indicates that the system does not support 64-bit compilation, you can find the VS Installation File for incremental installation.

[2] fatal error LNK1123: failed during COFF conversion: Invalid or corrupt file

This problem occurs when multiple VS versions are installed. The solution is as follows:

A)Microsoft official solution is to give VS2010 SP1 Patch, as follows: http://www.microsoft.com/zh-cn/download/details.aspx? Id = 23691. rebuild after installation. If not, try the next method.

B)Another solution officially released by Microsoft: http://support.microsoft.com/kb/320216/zh-cn. If the problem is embedded in the list, perform the following operations on the project and all dependent projects:

Right-click "Project Properties"> "configuration properties"> "configuration tool"> "input" and "output"> "embedded list" and select "no". Rebuild: solved successfully.

 

 

Code Attached: Http://files.cnblogs.com/CodeGuy/code.zip

 

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.