简体   繁体   中英

How to build a native Node.js extension using node-gyp on Win 7 x64 VS2012?

I tried to follow the tutorial how to build a native Node.js extension on Win 7 x64 (VS2012). I created a new directory for the extension and created the hello.cc and bindings.gyp files. Next, I opened a cmd and changed to the extension directory. I ran the following command:+

D:\git\ndext>node-gyp configure --msvs_version=2012
gyp info it worked if it ends with ok
gyp info using node-gyp@0.12.2
gyp info using node@0.10.25 | win32 | x64
gyp info spawn python
gyp info spawn args [ 'C:\\Users\\abc\\AppData\\Roaming\\npm\\node_modules\\node-gyp\\gyp\\gyp_main.py',
gyp info spawn args   'binding.gyp',
gyp info spawn args   '-f',
gyp info spawn args   'msvs',
gyp info spawn args   '-G',
gyp info spawn args   'msvs_version=2012',
gyp info spawn args   '-I',
gyp info spawn args   'D:\\git\\ndext\\build\\config.gypi',
gyp info spawn args   '-I',
gyp info spawn args   'C:\\Users\\abc\\AppData\\Roaming\\npm\\node_modules\\node-gyp\\addon.gypi',
gyp info spawn args   '-I',
gyp info spawn args   'C:\\Users\\abc\\.node-gyp\\0.10.25\\common.gypi',
gyp info spawn args   '-Dlibrary=shared_library',
gyp info spawn args   '-Dvisibility=default',
gyp info spawn args   '-Dnode_root_dir=C:\\Users\\abc\\.node-gyp\\0.10.25',
gyp info spawn args   '-Dmodule_root_dir=D:\\git\\ndext',
gyp info spawn args   '--depth=.',
gyp info spawn args   '--generator-output',
gyp info spawn args   'D:\\git\\ndext\\build',
gyp info spawn args   '-Goutput_dir=.' ]

After that I had a new directory build that contains the VS2012 sln and vcproj file. Trying to build this extension in VS2012 fails:

------ Build started: Project: hello, Configuration: Debug x64 ------
1>  hello.cc
1>C:\Users\abc\.node-gyp\0.10.25\deps\v8\include\v8.h(184): warning C4506: no definition for inline function 'v8::Persistent<T> v8::Persistent<T>::New(v8::Handle<T>)'
1>          with
1>          [
1>              T=v8::Object
1>          ]
1>LINK : fatal error LNK1104: cannot open file 'C:\Users\abc\.node-gyp\0.10.25\Debug\node.lib'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I understand the error (linker did not find the node.lib in the specified directory). The question is what went wrong with node-gyp configure? Did I forget another parameter?

But building on the cmd using node-gyp build runs successfully...

I finally found a solution that works (at least for me).

Building node.js from source:

  • Download the node.js source code from here .
  • Extract the node.js source code, open a command line terminal and change to the directory that contains the node.js source code.
  • Set the environment variable GYP_MSVS_VERSION to VS2012 by typing set GYP_MSVS_VERSION=2012 .
  • To build the x64 debug version, enter vcbuild.bat nosign debug x64 ; if you want to build the x64 release version, enter vcbuild.bat nosign release x64 . More details here .

After that, node.js has been successfully build. Also, the VS2012 node.sln and vcprojs have been created by vcbuild.bat. Building in VS2012 also works.

Building a native node.js extension I performed the following steps:

  • Open a command line terminal and change to the extension directory.
  • node-gyp assumes a default node_root_dir where it looks for all needed sources and libs. Unfortunately, in this default directory, the node.lib was not in the expected directory. Thus, I changed the used node_root_dir to the previous build node.js sources. Assuming the the node.js source directory is D:/git/node-v0.10.25/node-v0.10.25 , the command looks like follows:
node-gyp configure --nodedir="D:/git/node-v0.10.25/node-v0.10.25" --msvs_version=2012

Thus, I was able to build the debug and release version of the node.js extension within VS2012 successfully.

If you look at the code, you'll notice that you have #include <node.h> not #include <"node.h"> . This is because Node-GYP has the Node.js source as a native dep, not as a included file. To build directly from Visual Studio, you would need to somehow instruct it to have node.h (and associated files) available.

Basically, just use node-gyp build .

If you use everything, you will see node-gyp provides 3 node.lib. 在此处输入图片说明

So you can just set Release build in VS.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM