![](/img/trans.png)
[英]how to add dependence to static library in binding.gyp node-gyp for node.js extension
[英]How to build a native Node.js extension using node-gyp on Win 7 x64 VS2012?
我尝试按照教程如何在 Win 7 x64 (VS2012) 上构建原生 Node.js 扩展。 我为扩展创建了一个新目录并创建了 hello.cc 和 bindings.gyp 文件。 接下来,我打开一个cmd,切换到扩展目录。 我运行了以下命令:+
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=.' ]
之后,我有一个包含 VS2012 sln 和 vcproj 文件的新目录构建。 尝试在 VS2012 中构建此扩展失败:
------ 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 ==========
我了解错误(链接器未在指定目录中找到 node.lib)。 问题是 node-gyp 配置出了什么问题? 我忘记了另一个参数吗?
但是使用 node-gyp build 在 cmd 上构建运行成功......
我终于找到了一个有效的解决方案(至少对我而言)。
从源代码构建 node.js:
set GYP_MSVS_VERSION=2012
将环境变量GYP_MSVS_VERSION
set GYP_MSVS_VERSION=2012
。vcbuild.bat nosign debug x64
; 如果要构建 x64 发行版,请输入vcbuild.bat nosign release x64
。 更多细节在这里。之后,node.js 就构建成功了。 此外,VS2012 node.sln 和 vcprojs 已由 vcbuild.bat 创建。 在 VS2012 中构建也有效。
构建本机 node.js 扩展我执行了以下步骤:
node_root_dir
假定默认node_root_dir
用于查找所有需要的源和库。 不幸的是,在这个默认目录中,node.lib 不在预期目录中。 因此,我将使用的node_root_dir
更改为之前构建的 node.js 源。 假设 node.js 源目录为D:/git/node-v0.10.25/node-v0.10.25
,命令如下:node-gyp configure --nodedir="D:/git/node-v0.10.25/node-v0.10.25" --msvs_version=2012
因此,我能够在 VS2012 中成功构建 node.js 扩展的调试和发布版本。
如果您查看代码,您会注意到您使用的是#include <node.h>
而不是#include <"node.h">
。 这是因为 Node-GYP 将 Node.js 源代码作为本机 dep,而不是作为包含文件。 要直接从 Visual Studio 构建,您需要以某种方式指示它具有node.h
(和相关文件)。
基本上,只需使用node-gyp build
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.