[英]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.