"深入浅出nodejs 朴灵" 例子 c/c++扩展模块

http://diveintonode.org/

在作者的帮助下,build成功. 下面贴出的hello.cc和binding.gyp都是已经过时的代码了(暂时保留).

应该以官方最新例子(https://nodejs.org/docs/latest/api/addons.html)为准.

hello.cc

  1. #include <node.h>
  2. #include <v8.h>
  3.  
  4. using namespace v8;
  5.  
  6. Handle<Value> SayHello(const Arguments& args){
  7. HandleScope scope;
  8. return scope.Close(String::New("Hello world!"));
  9. }
  10.  
  11. void Init_Hello(Handle<Object> target){
  12. target->Set(String::NewSymbol("sayHello"), FunctionTemplate::New(SayHello)->GetFunction());
  13. }
  14.  
  15. NODE_MODULE(hello, Init_Hello)

binding.gyp

  1. {
  2. 'targets':[
  3. {
  4. 'target_name':'hello',
  5. 'sources':[
  6. './hello.cc'
  7. ],
  8. 'conditions':[
  9. ['OS == "win"',
  10. {
  11. 'libraries':['-lnode.lib']
  12. }
  13. ]
  14. ]
  15. }
  16. ]
  17. }

configure没有问题:

  1. C:\wxg\test\nodejs\dll_demo
  2. λ node-gyp configure
  3. gyp info it worked if it ends with ok
  4. gyp info using node-gyp@3.2.1
  5. gyp info using node@0.12.1 | win32 | x64
  6. gyp info spawn C:\Python27\python.EXE
  7. gyp info spawn args [ 'E:\\wxg\\Git\\nvmw\\v0.12.1\\node_modules\\node-gyp\\gyp\\gyp_main.py',
  8. gyp info spawn args 'binding.gyp',
  9. gyp info spawn args '-f',
  10. gyp info spawn args 'msvs',
  11. gyp info spawn args '-G',
  12. gyp info spawn args 'msvs_version=auto',
  13. gyp info spawn args '-I',
  14. gyp info spawn args 'C:\\wxg\\test\\nodejs\\dll_demo\\build\\config.gypi',
  15. gyp info spawn args '-I',
  16. gyp info spawn args 'E:\\wxg\\Git\\nvmw\\v0.12.1\\node_modules\\node-gyp\\addon.gypi',
  17. gyp info spawn args '-I',
  18. gyp info spawn args 'C:\\Users\\WXG\\.node-gyp\\0.12.1\\common.gypi',
  19. gyp info spawn args '-Dlibrary=shared_library',
  20. gyp info spawn args '-Dvisibility=default',
  21. gyp info spawn args '-Dnode_root_dir=C:\\Users\\WXG\\.node-gyp\\0.12.1',
  22. gyp info spawn args '-Dnode_gyp_dir=E:\\wxg\\Git\\nvmw\\v0.12.1\\node_modules\\node-gyp',
  23. gyp info spawn args '-Dnode_lib_file=node.lib',
  24. gyp info spawn args '-Dmodule_root_dir=C:\\wxg\\test\\nodejs\\dll_demo',
  25. gyp info spawn args '--depth=.',
  26. gyp info spawn args '--no-parallel',
  27. gyp info spawn args '--generator-output',
  28. gyp info spawn args 'C:\\wxg\\test\\nodejs\\dll_demo\\build',
  29. gyp info spawn args '-Goutput_dir=.' ]
  30. gyp info ok

build失败.

  1. C:\wxg\test\nodejs\dll_demo
  2. λ node-gyp build
  3. gyp info it worked if it ends with ok
  4. gyp info using node-gyp@3.2.1
  5. gyp info using node@0.12.1 | win32 | x64
  6. gyp info spawn c:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe
  7. gyp info spawn args [ 'build/binding.sln',
  8. gyp info spawn args '/clp:Verbosity=minimal',
  9. gyp info spawn args '/nologo',
  10. gyp info spawn args '/p:Configuration=Release;Platform=x64' ]
  11. 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]
  12. gyp ERR! build error
  13. gyp ERR! stack Error: `c:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe` failed with exit code: 1
  14. gyp ERR! stack at ChildProcess.onExit (E:\wxg\Git\nvmw\v0.12.1\node_modules\node-gyp\lib\build.js:276:23)
  15. gyp ERR! stack at ChildProcess.emit (events.js:110:17)
  16. gyp ERR! stack at Process.ChildProcess._handle.onexit (child_process.js:1067:12)
  17. gyp ERR! System Windows_NT 6.1.7601
  18. 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"
  19. gyp ERR! cwd C:\wxg\test\nodejs\dll_demo
  20. gyp ERR! node -v v0.12.1
  21. gyp ERR! node-gyp -v v3.2.1
  22. 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

  1. Windows 7/8:
  2. 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之后,仍然报错.记录如下:

  1. C:\wxg\test\nodejs\dll_demo
  2. λ node-gyp build
  3. gyp info it worked if it ends with ok
  4. gyp info using node-gyp@3.2.1
  5. gyp info using node@0.12.1 | win32 | x64
  6. gyp info spawn C:\Program Files (x86)\MSBuild\12.0\bin\msbuild.exe
  7. gyp info spawn args [ 'build/binding.sln',
  8. gyp info spawn args '/clp:Verbosity=minimal',
  9. gyp info spawn args '/nologo',
  10. gyp info spawn args '/p:Configuration=Release;Platform=x64' ]
  11. 在此解决方案中一次生成一个项目。若要启用并行生成,请添加“/m”开关。
  12. 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]
  13. gyp ERR! build error
  14. gyp ERR! stack Error: `C:\Program Files (x86)\MSBuild\12.0\bin\msbuild.exe` failed with exit code: 1
  15. gyp ERR! stack at ChildProcess.onExit (E:\wxg\Git\nvmw\v0.12.1\node_modules\node-gyp\lib\build.js:276:23)
  16. gyp ERR! stack at ChildProcess.emit (events.js:110:17)
  17. gyp ERR! stack at Process.ChildProcess._handle.onexit (child_process.js:1067:12)
  18. gyp ERR! System Windows_NT 6.1.7601
  19. 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"
  20. gyp ERR! cwd C:\wxg\test\nodejs\dll_demo
  21. gyp ERR! node -v v0.12.1
  22. gyp ERR! node-gyp -v v3.2.1
  23. gyp ERR! not ok
  24.  
  25. C:\wxg\test\nodejs\dll_demo
  26. λ

再次执行

λ node-gyp configure

  1. C:\wxg\test\nodejs\dll_demo
  2. λ node-gyp configure
  3. gyp info it worked if it ends with ok
  4. gyp info using node-gyp@3.2.1
  5. gyp info using node@0.12.1 | win32 | x64
  6. gyp info spawn C:\Python27\python.EXE
  7. gyp info spawn args [ 'E:\\wxg\\Git\\nvmw\\v0.12.1\\node_modules\\node-gyp\\gyp\\gyp_main.py',
  8. gyp info spawn args 'binding.gyp',
  9. gyp info spawn args '-f',
  10. gyp info spawn args 'msvs',
  11. gyp info spawn args '-G',
  12. gyp info spawn args 'msvs_version=auto',
  13. gyp info spawn args '-I',
  14. gyp info spawn args 'C:\\wxg\\test\\nodejs\\dll_demo\\build\\config.gypi',
  15. gyp info spawn args '-I',
  16. gyp info spawn args 'E:\\wxg\\Git\\nvmw\\v0.12.1\\node_modules\\node-gyp\\addon.gypi',
  17. gyp info spawn args '-I',
  18. gyp info spawn args 'C:\\Users\\WXG\\.node-gyp\\0.12.1\\common.gypi',
  19. gyp info spawn args '-Dlibrary=shared_library',
  20. gyp info spawn args '-Dvisibility=default',
  21. gyp info spawn args '-Dnode_root_dir=C:\\Users\\WXG\\.node-gyp\\0.12.1',
  22. gyp info spawn args '-Dnode_gyp_dir=E:\\wxg\\Git\\nvmw\\v0.12.1\\node_modules\\node-gyp',
  23. gyp info spawn args '-Dnode_lib_file=node.lib',
  24. gyp info spawn args '-Dmodule_root_dir=C:\\wxg\\test\\nodejs\\dll_demo',
  25. gyp info spawn args '--depth=.',
  26. gyp info spawn args '--no-parallel',
  27. gyp info spawn args '--generator-output',
  28. gyp info spawn args 'C:\\wxg\\test\\nodejs\\dll_demo\\build',
  29. gyp info spawn args '-Goutput_dir=.' ]
  30. gyp info okZ

再次执行

λ node-gyp build

  1. C:\wxg\test\nodejs\dll_demo
  2. λ node-gyp build
  3. gyp info it worked if it ends with ok
  4. gyp info using node-gyp@3.2.1
  5. gyp info using node@0.12.1 | win32 | x64
  6. gyp info spawn C:\Program Files (x86)\MSBuild\12.0\bin\msbuild.exe
  7. gyp info spawn args [ 'build/binding.sln',
  8. gyp info spawn args '/clp:Verbosity=minimal',
  9. gyp info spawn args '/nologo',
  10. gyp info spawn args '/p:Configuration=Release;Platform=x64' ]
  11. 在此解决方案中一次生成一个项目。若要启用并行生成,请添加“/m”开关。
  12. hello.cc
  13. ..\hello.cc(6): error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int [C:\wxg\test\nodejs\dll_demo\build\hello.vcxproj]
  14. ..\hello.cc(6): error C2143: 语法错误 : 缺少“,”(在“&”的前面) [C:\wxg\test\nodejs\dll_demo\build\hello.vcxproj]
  15. ..\hello.cc(7): error C2248: v8::HandleScope::HandleScope”: 无法访问 protected 成员(在“v8::HandleScope”类中声明) [C:\wxg\test\nodejs\dll_demo\build\hello.vcxproj]
  16. C:\Users\WXG\.node-gyp\0.12.1\deps\v8\include\v8.h(816) : 参见“v8::HandleScope::HandleScope”的声明
  17. C:\Users\WXG\.node-gyp\0.12.1\deps\v8\include\v8.h(800) : 参见“v8::HandleScope”的声明
  18. ..\hello.cc(8): error C2039: Close”: 不是“v8::HandleScope”的成员 [C:\wxg\test\nodejs\dll_demo\build\hello.vcxproj]
  19. C:\Users\WXG\.node-gyp\0.12.1\deps\v8\include\v8.h(800) : 参见“v8::HandleScope”的声明
  20. ..\hello.cc(8): error C2039: New”: 不是“v8::String”的成员 [C:\wxg\test\nodejs\dll_demo\build\hello.vcxproj]
  21. C:\Users\WXG\.node-gyp\0.12.1\deps\v8\include\v8.h(1599) : 参见“v8::String”的声明
  22. ..\hello.cc(8): error C3861: New”: 找不到标识符 [C:\wxg\test\nodejs\dll_demo\build\hello.vcxproj]
  23. ..\hello.cc(12): error C2039: NewSymbol”: 不是“v8::String”的成员 [C:\wxg\test\nodejs\dll_demo\build\hello.vcxproj]
  24. C:\Users\WXG\.node-gyp\0.12.1\deps\v8\include\v8.h(1599) : 参见“v8::String”的声明
  25. ..\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:
  26. :Hand
  27. le<v8::Value> (__cdecl *)(const int)”转换为“v8::Isolate *” [C:\wxg\test\nodejs\dll_demo\build\hello.vcxproj]
  28. 没有使该转换得以执行的上下文
  29. ..\hello.cc(12): error C2227: “->GetFunction”的左边必须指向类/结构/联合/泛型类型 [C:\wxg\test\nodejs\dll_demo\build\hello.vcxproj]
  30. ..\hello.cc(12): error C3861: NewSymbol”: 找不到标识符 [C:\wxg\test\nodejs\dll_demo\build\hello.vcxproj]
  31. gyp ERR! build error
  32. gyp ERR! stack Error: `C:\Program Files (x86)\MSBuild\12.0\bin\msbuild.exe` failed with exit code: 1
  33. gyp ERR! stack at ChildProcess.onExit (E:\wxg\Git\nvmw\v0.12.1\node_modules\node-gyp\lib\build.js:276:23)
  34. gyp ERR! stack at ChildProcess.emit (events.js:110:17)
  35. gyp ERR! stack at Process.ChildProcess._handle.onexit (child_process.js:1067:12)
  36. gyp ERR! System Windows_NT 6.1.7601
  37. 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"
  38. gyp ERR! cwd C:\wxg\test\nodejs\dll_demo
  39. gyp ERR! node -v v0.12.1
  40. gyp ERR! node-gyp -v v3.2.1
  41. gyp ERR! not ok

build 仍然失败. 看提示貌似是hello.cc文件编写错误.目前没能力搞定.先放放.

==============================================================

在作者的帮助下,build成功.

  1. C++的接口一直在变化,参考:https://nodejs.org/docs/latest/api/addons.html

按照官方给的例子,修改hello.cc和binding.gyp,再依次执行node-gyp configure , node-gyp build 就可以了.

  1. C:\wxg\test\nodejs\dll_demo
  2. λ node-gyp build
  3. gyp info it worked if it ends with ok
  4. gyp info using node-gyp@3.2.1
  5. gyp info using node@0.12.1 | win32 | x64
  6. gyp info spawn C:\Program Files (x86)\MSBuild\12.0\bin\msbuild.exe
  7. gyp info spawn args [ 'build/binding.sln',
  8. gyp info spawn args '/clp:Verbosity=minimal',
  9. gyp info spawn args '/nologo',
  10. gyp info spawn args '/p:Configuration=Release;Platform=x64' ]
  11. 在此解决方案中一次生成一个项目。若要启用并行生成,请添加“/m”开关。
  12. hello.cc
  13. win_delay_load_hook.c
  14. 正在创建库 C:\wxg\test\nodejs\dll_demo\build\Release\addon.lib 和对象 C:\wxg\test\nodejs\dll_demo\build\Re
  15. lease\a
  16. ddon.exp
  17. 正在生成代码
  18. 已完成代码的生成
  19. addon.vcxproj -> C:\wxg\test\nodejs\dll_demo\build\Release\\addon.node
  20. gyp info ok
  21.  
  22. C:\wxg\test\nodejs\dll_demo
  23. λ node hello.js
  24. world

  

node c/c++扩展模块build失败.的更多相关文章

  1. TeamCity : Build 失败条件

    允许用户配置 Build 失败的条件是很有用的功能,它是我们配置复杂 Build 流程的基础.TeamCity 为用户自定义 Build 失败条件提供了很好的支持.这些条件大体上可以分为两类,分别是: ...

  2. No compiler is provided in this environment. --Maven build失败

    今天,maven build 失败了, 遇到下面的问题 经过查找,通过这个大佬的blog(  https://blog.csdn.net/lslk9898/article/details/738367 ...

  3. IDEA配置好maven后新建maven项目一直build失败的解决方法

    maven配置了aliyun中央仓库后,IDEA新建maven项目一直出现以下问题: 相信有遇到这个问题的小伙伴很蛋疼,明明maven配置没错,新建项目却一直build失败,为了这个问题我重装过几次I ...

  4. 【vue】解决npm run build失败问题

    问题说明: 首先,npm run build失败的原因大部分都是因为webpack的子库和webpack的版本号不对应的问题

  5. node.js npm 安装spm失败,竟然是版本的问题

    SPM v.1.1.2 With SeaJS   SPM v1.1.2使用指南 1.SPM用途 SeaJS提供了模块化开发的机制,在代码开发完后,还需要做产品发布相关的一些操作. 这些可以通过SPM来 ...

  6. Jenkins build失败条件

    在Jenkins 项目写了很多剧本.有时候,我发现脚本失败,但Jenkins运行成功. Jenkins无论是通过退出代码0比量build成功. 因此,newLISP在.您可以使用(exit)对于成功. ...

  7. maven web项目build失败

    通过maven build发布web项目到tomcat时报如下异常: [INFO] ---------------------------------------------------------- ...

  8. 安装node.msi 格式的文件失败

    in10 安装node.msi格式的文件,出现2503 2502 错误码, win+x 打开 在命令提示符窗口中输入: msiexec /package "安装msi格式的文件的全路径&qu ...

  9. maven build 失败

    (1).之前项目打包都OK,不知道咋回事,突然出现如下图的问题: (2)上网查找了好多,一直没有解决,最后把本地maven库删除,重新运行了一遍,变成BUILD  SUCCESS,但是警告还是The ...

随机推荐

  1. [一位菜鸟的COCOS-2D编程之路]精灵表单的制作以及简易动画的生成

    1.第一步:使用Zwoptex 制作精灵表单 2.制作的表单的名称为 cocos2Dpng,cocos2D.plist: 3.精灵的动画效果 主要分为五部分. // on "init&quo ...

  2. st_MES_InsertIntoSalaryManage

    USE [ChiefmesNew]GO/****** Object: StoredProcedure [dbo].[st_MES_InsertIntoSalaryManage] Script Date ...

  3. 关于Windows文件名和路径名的那些事

    博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:关于Windows文件名和路径名的那些事.

  4. Notes(一)

    Numerous experimental measurements in spatially complex systems have revealed anomalous diffusion in ...

  5. 新浪旗下的SAE云服务入门

    前言: 云服务已经火热很多年了,到了现在已经基本进入稳定期,稍微有实力的公司都会推出免费的云存储,像百度的还几个T,速度也不错.如果不担心数据安全问题,代替平时的U盘还是没有问题.而SAE是新浪在20 ...

  6. Oracle VM Virtual Box 4.3 小巧精悍的虚拟机软件

    https://www.virtualbox.org/wiki/Downloads Download VirtualBox Here, you will find links to VirtualBo ...

  7. android学习日记03--常用控件checkbox/radiobutton

    常用控件3.checkbox 复选框,确定是否勾选,点击一下勾选,点击第二下取消,当有一系列备选项时适合用checkbox控件,方便用户提交数据. 贴上例子Activity的java代码 packag ...

  8. 运维知识体系v0.5

    http://www.90qj.com/?post=318http://ixdba.blog.51cto.com/2895551/1751377   运维知识体系v0.5-(运维社区-赵班长出品,欢迎 ...

  9. 文件I/O之sync、fsync和fdatasync函数

    传统的UNIX实现在内核中设有缓冲区高速缓存或页面高速缓存,大多数磁盘I/O都通过缓冲进行.当将数据写入文件时,内核通常先将数据复制到其中一个缓冲区中,如果 该缓冲区尚未写满,则并不将其排入输出队列, ...

  10. VM11安装Mac OS X 10.10

    工具/原料 1. VMware Workstation 11.12 2. unlocker 206(for OS X 插件补丁) 3. Mac OS X 10.10镜像 方法/步骤   有图有真相,哈 ...