"深入浅出nodejs 朴灵" 例子 c/c++扩展模块
http://diveintonode.org/
在作者的帮助下,build成功. 下面贴出的hello.cc和binding.gyp都是已经过时的代码了(暂时保留).
应该以官方最新例子(https://nodejs.org/docs/latest/api/addons.html)为准.
hello.cc
#include <node.h>
#include <v8.h> using namespace v8; Handle<Value> SayHello(const Arguments& args){
HandleScope scope;
return scope.Close(String::New("Hello world!"));
} void Init_Hello(Handle<Object> target){
target->Set(String::NewSymbol("sayHello"), FunctionTemplate::New(SayHello)->GetFunction());
} NODE_MODULE(hello, Init_Hello)
binding.gyp
{
'targets':[
{
'target_name':'hello',
'sources':[
'./hello.cc'
],
'conditions':[
['OS == "win"',
{
'libraries':['-lnode.lib']
}
]
]
}
]
}
configure没有问题:
C:\wxg\test\nodejs\dll_demo
λ node-gyp configure
gyp info it worked if it ends with ok
gyp info using [email protected]
gyp info using [email protected] | win32 | x64
gyp info spawn C:\Python27\python.EXE
gyp info spawn args [ 'E:\\wxg\\Git\\nvmw\\v0.12.1\\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=auto',
gyp info spawn args '-I',
gyp info spawn args 'C:\\wxg\\test\\nodejs\\dll_demo\\build\\config.gypi',
gyp info spawn args '-I',
gyp info spawn args 'E:\\wxg\\Git\\nvmw\\v0.12.1\\node_modules\\node-gyp\\addon.gypi',
gyp info spawn args '-I',
gyp info spawn args 'C:\\Users\\WXG\\.node-gyp\\0.12.1\\common.gypi',
gyp info spawn args '-Dlibrary=shared_library',
gyp info spawn args '-Dvisibility=default',
gyp info spawn args '-Dnode_root_dir=C:\\Users\\WXG\\.node-gyp\\0.12.1',
gyp info spawn args '-Dnode_gyp_dir=E:\\wxg\\Git\\nvmw\\v0.12.1\\node_modules\\node-gyp',
gyp info spawn args '-Dnode_lib_file=node.lib',
gyp info spawn args '-Dmodule_root_dir=C:\\wxg\\test\\nodejs\\dll_demo',
gyp info spawn args '--depth=.',
gyp info spawn args '--no-parallel',
gyp info spawn args '--generator-output',
gyp info spawn args 'C:\\wxg\\test\\nodejs\\dll_demo\\build',
gyp info spawn args '-Goutput_dir=.' ]
gyp info ok
build失败.
C:\wxg\test\nodejs\dll_demo
λ node-gyp build
gyp info it worked if it ends with ok
gyp info using [email protected]
gyp info using [email protected] | win32 | x64
gyp info spawn c:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe
gyp info spawn args [ 'build/binding.sln',
gyp info spawn args '/clp:Verbosity=minimal',
gyp info spawn args '/nologo',
gyp info spawn args '/p:Configuration=Release;Platform=x64' ]
VCBUILD : error : project file 'C:\wxg\test\nodejs\dll_demo\build\hello.vcproj' was not found or not a valid p roject file. [C:\wxg\test\nodejs\dll_demo\build\binding.sln]
gyp ERR! build error
gyp ERR! stack Error: `c:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe` failed with exit code: 1
gyp ERR! stack at ChildProcess.onExit (E:\wxg\Git\nvmw\v0.12.1\node_modules\node-gyp\lib\build.js:276:23)
gyp ERR! stack at ChildProcess.emit (events.js:110:17)
gyp ERR! stack at Process.ChildProcess._handle.onexit (child_process.js:1067:12)
gyp ERR! System Windows_NT 6.1.7601
gyp ERR! command "E:\\wxg\\Git\\nvmw\\v0.12.1\\\\node.exe" "E:\\wxg\\Git\\nvmw\\v0.12.1\\node_modules\\node-gyp\\bin\\node-gyp.js" "build"
gyp ERR! cwd C:\wxg\test\nodejs\dll_demo
gyp ERR! node -v v0.12.1
gyp ERR! node-gyp -v v3.2.1
gyp ERR! not ok
在网上搜索了下: http://www.imooc.com/qadetail/38800 , http://stackoverflow.com/questions/32964144/gyp-err-stack-error-c-program-files-x86-msbuild-12-0-bin-msbuild-exe-fail ,
最靠谱的应该还是 https://www.npmjs.com/package/node-gyp
Windows 7/8:
Microsoft Visual Studio C++ 2013 for Windows Desktop (Express version works well)
但是,我还没装"Microsoft Visual Studio C++ 2013", studio文件太大,下载要很久. 我在网上找的一个"Microsoft Visual C++ 2013运行库",安装了,没效果. 这个等以后下载了studio再验证下.
==============================================================================
在安装了MS studio 2013之后,仍然报错.记录如下:
C:\wxg\test\nodejs\dll_demo
λ node-gyp build
gyp info it worked if it ends with ok
gyp info using [email protected]
gyp info using [email protected] | win32 | x64
gyp info spawn C:\Program Files (x86)\MSBuild\12.0\bin\msbuild.exe
gyp info spawn args [ 'build/binding.sln',
gyp info spawn args '/clp:Verbosity=minimal',
gyp info spawn args '/nologo',
gyp info spawn args '/p:Configuration=Release;Platform=x64' ]
在此解决方案中一次生成一个项目。若要启用并行生成,请添加“/m”开关。
VCBUILD : error : project file 'C:\wxg\test\nodejs\dll_demo\build\hello.vcproj' was not found or not a valid p roject file. [C:\wxg\test\nodejs\dll_demo\build\binding.sln]
gyp ERR! build error
gyp ERR! stack Error: `C:\Program Files (x86)\MSBuild\12.0\bin\msbuild.exe` failed with exit code: 1
gyp ERR! stack at ChildProcess.onExit (E:\wxg\Git\nvmw\v0.12.1\node_modules\node-gyp\lib\build.js:276:23)
gyp ERR! stack at ChildProcess.emit (events.js:110:17)
gyp ERR! stack at Process.ChildProcess._handle.onexit (child_process.js:1067:12)
gyp ERR! System Windows_NT 6.1.7601
gyp ERR! command "E:\\wxg\\Git\\nvmw\\v0.12.1\\\\node.exe" "E:\\wxg\\Git\\nvmw\\v0.12.1\\node_modules\\node-gyp\\bin\\node-gyp.js" "build"
gyp ERR! cwd C:\wxg\test\nodejs\dll_demo
gyp ERR! node -v v0.12.1
gyp ERR! node-gyp -v v3.2.1
gyp ERR! not ok C:\wxg\test\nodejs\dll_demo
λ
再次执行
λ node-gyp configure
C:\wxg\test\nodejs\dll_demo
λ node-gyp configure
gyp info it worked if it ends with ok
gyp info using [email protected]
gyp info using [email protected] | win32 | x64
gyp info spawn C:\Python27\python.EXE
gyp info spawn args [ 'E:\\wxg\\Git\\nvmw\\v0.12.1\\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=auto',
gyp info spawn args '-I',
gyp info spawn args 'C:\\wxg\\test\\nodejs\\dll_demo\\build\\config.gypi',
gyp info spawn args '-I',
gyp info spawn args 'E:\\wxg\\Git\\nvmw\\v0.12.1\\node_modules\\node-gyp\\addon.gypi',
gyp info spawn args '-I',
gyp info spawn args 'C:\\Users\\WXG\\.node-gyp\\0.12.1\\common.gypi',
gyp info spawn args '-Dlibrary=shared_library',
gyp info spawn args '-Dvisibility=default',
gyp info spawn args '-Dnode_root_dir=C:\\Users\\WXG\\.node-gyp\\0.12.1',
gyp info spawn args '-Dnode_gyp_dir=E:\\wxg\\Git\\nvmw\\v0.12.1\\node_modules\\node-gyp',
gyp info spawn args '-Dnode_lib_file=node.lib',
gyp info spawn args '-Dmodule_root_dir=C:\\wxg\\test\\nodejs\\dll_demo',
gyp info spawn args '--depth=.',
gyp info spawn args '--no-parallel',
gyp info spawn args '--generator-output',
gyp info spawn args 'C:\\wxg\\test\\nodejs\\dll_demo\\build',
gyp info spawn args '-Goutput_dir=.' ]
gyp info okZ
再次执行
λ node-gyp build
C:\wxg\test\nodejs\dll_demo
λ node-gyp build
gyp info it worked if it ends with ok
gyp info using [email protected]
gyp info using [email protected] | win32 | x64
gyp info spawn C:\Program Files (x86)\MSBuild\12.0\bin\msbuild.exe
gyp info spawn args [ 'build/binding.sln',
gyp info spawn args '/clp:Verbosity=minimal',
gyp info spawn args '/nologo',
gyp info spawn args '/p:Configuration=Release;Platform=x64' ]
在此解决方案中一次生成一个项目。若要启用并行生成,请添加“/m”开关。
hello.cc
..\hello.cc(6): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int [C:\wxg\test\nodejs\dll_demo\build\hello.vcxproj]
..\hello.cc(6): error C2143: 语法错误 : 缺少“,”(在“&”的前面) [C:\wxg\test\nodejs\dll_demo\build\hello.vcxproj]
..\hello.cc(7): error C2248: “v8::HandleScope::HandleScope”: 无法访问 protected 成员(在“v8::HandleScope”类中声明) [C:\wxg\test\nodejs\dll_demo\build\hello.vcxproj]
C:\Users\WXG\.node-gyp\0.12.1\deps\v8\include\v8.h(816) : 参见“v8::HandleScope::HandleScope”的声明
C:\Users\WXG\.node-gyp\0.12.1\deps\v8\include\v8.h(800) : 参见“v8::HandleScope”的声明
..\hello.cc(8): error C2039: “Close”: 不是“v8::HandleScope”的成员 [C:\wxg\test\nodejs\dll_demo\build\hello.vcxproj]
C:\Users\WXG\.node-gyp\0.12.1\deps\v8\include\v8.h(800) : 参见“v8::HandleScope”的声明
..\hello.cc(8): error C2039: “New”: 不是“v8::String”的成员 [C:\wxg\test\nodejs\dll_demo\build\hello.vcxproj]
C:\Users\WXG\.node-gyp\0.12.1\deps\v8\include\v8.h(1599) : 参见“v8::String”的声明
..\hello.cc(8): error C3861: “New”: 找不到标识符 [C:\wxg\test\nodejs\dll_demo\build\hello.vcxproj]
..\hello.cc(12): error C2039: “NewSymbol”: 不是“v8::String”的成员 [C:\wxg\test\nodejs\dll_demo\build\hello.vcxproj]
C:\Users\WXG\.node-gyp\0.12.1\deps\v8\include\v8.h(1599) : 参见“v8::String”的声明
..\hello.cc(12): error C2664: “v8::Local<v8::FunctionTemplate> v8::FunctionTemplate::New(v8::Isolate *,v8::FunctionCallback,v8::Handle<v8::Value>,v8::Handle<v8::Signature>,int)”: 无法将参数 1 从“v8:
:Hand
le<v8::Value> (__cdecl *)(const int)”转换为“v8::Isolate *” [C:\wxg\test\nodejs\dll_demo\build\hello.vcxproj]
没有使该转换得以执行的上下文
..\hello.cc(12): error C2227: “->GetFunction”的左边必须指向类/结构/联合/泛型类型 [C:\wxg\test\nodejs\dll_demo\build\hello.vcxproj]
..\hello.cc(12): error C3861: “NewSymbol”: 找不到标识符 [C:\wxg\test\nodejs\dll_demo\build\hello.vcxproj]
gyp ERR! build error
gyp ERR! stack Error: `C:\Program Files (x86)\MSBuild\12.0\bin\msbuild.exe` failed with exit code: 1
gyp ERR! stack at ChildProcess.onExit (E:\wxg\Git\nvmw\v0.12.1\node_modules\node-gyp\lib\build.js:276:23)
gyp ERR! stack at ChildProcess.emit (events.js:110:17)
gyp ERR! stack at Process.ChildProcess._handle.onexit (child_process.js:1067:12)
gyp ERR! System Windows_NT 6.1.7601
gyp ERR! command "E:\\wxg\\Git\\nvmw\\v0.12.1\\\\node.exe" "E:\\wxg\\Git\\nvmw\\v0.12.1\\node_modules\\node-gyp\\bin\\node-gyp.js" "build"
gyp ERR! cwd C:\wxg\test\nodejs\dll_demo
gyp ERR! node -v v0.12.1
gyp ERR! node-gyp -v v3.2.1
gyp ERR! not ok
build 仍然失败. 看提示貌似是hello.cc文件编写错误.目前没能力搞定.先放放.
==============================================================
在作者的帮助下,build成功.
C++的接口一直在变化,参考:https://nodejs.org/docs/latest/api/addons.html
按照官方给的例子,修改hello.cc和binding.gyp,再依次执行node-gyp configure , node-gyp build 就可以了.
C:\wxg\test\nodejs\dll_demo
λ node-gyp build
gyp info it worked if it ends with ok
gyp info using [email protected]
gyp info using [email protected] | win32 | x64
gyp info spawn C:\Program Files (x86)\MSBuild\12.0\bin\msbuild.exe
gyp info spawn args [ 'build/binding.sln',
gyp info spawn args '/clp:Verbosity=minimal',
gyp info spawn args '/nologo',
gyp info spawn args '/p:Configuration=Release;Platform=x64' ]
在此解决方案中一次生成一个项目。若要启用并行生成,请添加“/m”开关。
hello.cc
win_delay_load_hook.c
正在创建库 C:\wxg\test\nodejs\dll_demo\build\Release\addon.lib 和对象 C:\wxg\test\nodejs\dll_demo\build\Re
lease\a
ddon.exp
正在生成代码
已完成代码的生成
addon.vcxproj -> C:\wxg\test\nodejs\dll_demo\build\Release\\addon.node
gyp info ok C:\wxg\test\nodejs\dll_demo
λ node hello.js
world