zoukankan      html  css  js  c++  java
  • VS2013创建Node.js C++ Addons的过程

    首先看我的Node.js版本。

    node –v

    v6.11.4

    然后参照这篇文章来做:

    https://nodejs.org/api/addons.html#addons_hello_world

    1. 安装node-gyp

    npm install -g node-gyp

     

    1. 创建一个文件夹,在里面创建hello.cc文件

    // hello.cc

    #include <node.h>

     

    namespace demo {

     

    using v8::FunctionCallbackInfo;

    using v8::Isolate;

    using v8::Local;

    using v8::Object;

    using v8::String;

    using v8::Value;

     

    void Method(const FunctionCallbackInfo<Value>& args){

    Isolate* isolate = args.GetIsolate();

    args.GetReturnValue().Set(String::NewFromUtf8(isolate,"world"));

    }

     

    void init(Local<Object> exports){

    NODE_SET_METHOD(exports,"hello", Method);

    }

     

    NODE_MODULE(NODE_GYP_MODULE_NAME, init)

     

    }// namespace demo

     

    原文的说明如下:

    Note that all Node.js Addons must export an initialization function following the pattern:

    void Initialize(Local<Object> exports);
    NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)

     

    1. 创建binding.gyp文件
    {
    "targets":[
    {
    "target_name":"addon",
    "sources":["hello.cc"]
    }
    ]
    }

     

    1. 将cmd定位到当前的文件夹,然后运行命令:

    node-gyp configure

     

    结果报如下的错误:

    gyp info it worked if it ends with ok

    gyp info using node-gyp@3.6.2

    gyp info using node@6.11.4 | win32 | x64

    gyp ERR! configure error

    gyp ERR! stack Error: Can't find Python executable "python", you can set the PYTHON env variable.

    gyp ERR! stack at Object.failNoPython (C:UsersAdministratorAppDataRoaming pm ode_modules ode-gyplibconfigur

    e.js:483:19)

    gyp ERR! stack at Object.<anonymous> (C:UsersAdministratorAppDataRoaming pm ode_modules ode-gyplibconfigure

    .js:508:16)

    gyp ERR! stack at C:UsersAdministratorAppDataRoaming pm ode_modules ode-gyp ode_modulesgraceful-fspolyfill

    s.js:284:29

    gyp ERR! stack at FSReqWrap.oncomplete (fs.js:123:15)

    gyp ERR! System Windows_NT 6.1.7601

    gyp ERR! command "C:\Program Files\nodejs\node.exe" "C:\Users\Administrator\AppData\Roaming\npm\node_modules\n

    ode-gyp\bin\node-gyp.js" "configure"

    gyp ERR! cwd C:SourceCodeHenryProjectsHenryNodeTestCppAddonsdemo

    gyp ERR! node -v v6.11.4

    gyp ERR! node-gyp -v v3.6.2

    gyp ERR! not ok

     

    由于我的机器没有装python, 所以这里报错。

    我是从这个网站下载的python:

    https://www.python.org/downloads/windows/

    下载后点击安装即可,默认的安装路径是AppData的目录,我觉得不太好,就自定义选择了C:Python目录。

    安装完成后将python.exe所在的路径加入到环境变量中去(我只加了用户的环境变量,没有加系统的环境变量)。

     

    然后重新运行命令:

    node-gyp configure

     

    运行后还是报错:

    gyp info it worked if it ends with ok

    gyp info using node-gyp@3.6.2

    gyp info using node@6.11.4 | win32 | x64

    gyp ERR! configure error

    gyp ERR! stack Error: Can't find Python executable "C:PythonPython36", you can set the PYTHON env variable.

    gyp ERR! stack at Object.failNoPython (C:UsersAdministratorAppDataRoaming pm ode_modules ode-gyplibconfigur

    e.js:483:19)

    gyp ERR! stack at Object.<anonymous> (C:UsersAdministratorAppDataRoaming pm ode_modules ode-gyplibconfigure

    .js:508:16)

    gyp ERR! stack at C:UsersAdministratorAppDataRoaming pm ode_modules ode-gyp ode_modulesgraceful-fspolyfill

    s.js:284:29

    gyp ERR! stack at FSReqWrap.oncomplete (fs.js:123:15)

    gyp ERR! System Windows_NT 6.1.7601

    gyp ERR! command "C:\Program Files\nodejs\node.exe" "C:\Users\Administrator\AppData\Roaming\npm\node_modules\n

    ode-gyp\bin\node-gyp.js" "configure"

    gyp ERR! cwd C:SourceCodeHenryProjectsHenryNodeTestCppAddonsdemo

    gyp ERR! node -v v6.11.4

    gyp ERR! node-gyp -v v3.6.2

    gyp ERR! not ok

     

    只是错误不同了。于是又加了系统的环境变量,还是不行。

    看错误提示是在C:PythonPython36目录下找不到Phthon的执行文件,可是明明在那里啊。

     

    那就下载个32位的python试试吧。

    装完后修改PYTHON环境变量到32位的目录下:

    结果还是报同样的错误,服了!

    网上搜了一下,发现需要把Python的路径加到PATH环境变量中去才可以,于是照做了一下:

    同时需要将以前加的PYTHON环境变量移除(这个很重要,否则还是会报一样的错误)并且要重新启动cmd.

     

    这次报了另一个错误:

    gyp info it worked if it ends with ok

    gyp info using node-gyp@3.6.2

    gyp info using node@6.11.4 | win32 | x64

    gyp ERR! configure error

    gyp ERR! stack Error: Can't find Python executable "C:PythonPython36python.EXE", you can set the PYTHON env variable.

     

    gyp ERR! stack at Object.failNoPython (C:UsersAdministratorAppDataRoaming pm ode_modules ode-gyplibconfigur

    e.js:483:19)

    gyp ERR! stack at Object.<anonymous> (C:UsersAdministratorAppDataRoaming pm ode_modules ode-gyplibconfigure

    .js:508:16)

    gyp ERR! stack at C:UsersAdministratorAppDataRoaming pm ode_modules ode-gyp ode_modulesgraceful-fspolyfill

    s.js:284:29

    gyp ERR! stack at FSReqWrap.oncomplete (fs.js:123:15)

    gyp ERR! System Windows_NT 6.1.7601

    gyp ERR! command "C:\Program Files\nodejs\node.exe" "C:\Users\Administrator\AppData\Roaming\npm\node_modules\n

    ode-gyp\bin\node-gyp.js" "configure"

    gyp ERR! cwd C:SourceCodeHenryProjectsHenryNodeTestCppAddonsdemo

    gyp ERR! node -v v6.11.4

    gyp ERR! node-gyp -v v3.6.2

    gyp ERR! not ok

     

    难道是大小写有关系?把目录下的python.exe改成大写试试?

     

    还是一样的错误,服了!

    又搜了一下,有的说要设置单独的PYTHON环境变量,然后把%PYTHON%加到PATH的末尾。试了一下,还是报如下的错误:

     

    gyp info it worked if it ends with ok

    gyp info using node-gyp@3.6.2

    gyp info using node@6.11.4 | win32 | x64

    gyp ERR! configure error

    gyp ERR! stack Error: Can't find Python executable "C:PythonPython36", you can set the PYTHON env variable.

    gyp ERR! stack at Object.failNoPython (C:UsersAdministratorAppDataRoaming pm ode_modules ode-gyplibconfigur

    e.js:483:19)

    gyp ERR! stack at Object.<anonymous> (C:UsersAdministratorAppDataRoaming pm ode_modules ode-gyplibconfigure

    .js:508:16)

    gyp ERR! stack at C:UsersAdministratorAppDataRoaming pm ode_modules ode-gyp ode_modulesgraceful-fspolyfill

    s.js:284:29

    gyp ERR! stack at FSReqWrap.oncomplete (fs.js:123:15)

    gyp ERR! System Windows_NT 6.1.7601

    gyp ERR! command "C:\Program Files\nodejs\node.exe" "C:\Users\Administrator\AppData\Roaming\npm\node_modules\n

    ode-gyp\bin\node-gyp.js" "configure"

    gyp ERR! cwd C:SourceCodeHenryProjectsHenryNodeTestCppAddonsdemo

    gyp ERR! node -v v6.11.4

    gyp ERR! node-gyp -v v3.6.2

    gyp ERR! not ok

     

    又搜了一下,有的说是需要用管理员权限来运行命令行,检查了一下,我当前就是用管理员身份运行的命令行,所以这个对我没有帮助。

    有搜到这段描述:

    I first had Python version 3.6 for Windows installed on a Windows Server 2016 machine. I used the windows executable installation.

    This was required in order to install the web3 package.

    But no matter how I set path and environment variables, during the npm install of web3 it kept complaining it couldn't find the python executable. Typing python in the command prompt worked fine.

    I suspect it has to do with version/compatibility issues between web3 and python. After uninstalling the 3.6 version of python and installing 2.7 using the npm command ArminMa suggested it all works!

    I did update my environment variables (PYTHON and PATH) to the 2.7 executable.

     

    说的是可能安装了两个版本的PYTHON, 导致了互相冲突,卸载掉一个就好了。于是试着把32位的卸载掉看看。

    结果还是同样的错误。于是把64位的PYTHON卸载掉,装上32位的,同时PYTHON环境变量也改一下。

    还是一样的错误。

    难道必须是PYTHON2? 好吧,卸载掉PYTHON3, 装PYTHON2试试吧。

    先下载64位的试试。

     

    安装完后环境变量也改一下。

     

    再次运行,这次多走了几步,Python的问题解决了。是另外的错误:

    gyp info it worked if it ends with ok

    gyp info using node-gyp@3.6.2

    gyp info using node@6.11.4 | win32 | x64

    gyp http GET https://nodejs.org/download/release/v6.11.4/node-v6.11.4-headers.tar.gz

    gyp http 200 https://nodejs.org/download/release/v6.11.4/node-v6.11.4-headers.tar.gz

    gyp http GET https://nodejs.org/download/release/v6.11.4/SHASUMS256.txt

    gyp http GET https://nodejs.org/download/release/v6.11.4/win-x86/node.lib

    gyp http GET https://nodejs.org/download/release/v6.11.4/win-x64/node.lib

    gyp http 200 https://nodejs.org/download/release/v6.11.4/win-x64/node.lib

    gyp http 200 https://nodejs.org/download/release/v6.11.4/SHASUMS256.txt

    gyp http 200 https://nodejs.org/download/release/v6.11.4/win-x86/node.lib

    gyp info spawn C:Python27python.EXE

    gyp info spawn args [ 'C:\Users\Administrator\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=2015',

    gyp info spawn args '-I',

    gyp info spawn args 'C:\SourceCode\HenryProjects\HenryNodeTest\CppAddons\demo\build\config.gypi',

    gyp info spawn args '-I',

    gyp info spawn args 'C:\Users\Administrator\AppData\Roaming\npm\node_modules\node-gyp\addon.gypi',

    gyp info spawn args '-I',

    gyp info spawn args 'C:\Users\Administrator\.node-gyp\6.11.4\include\node\common.gypi',

    gyp info spawn args '-Dlibrary=shared_library',

    gyp info spawn args '-Dvisibility=default',

    gyp info spawn args '-Dnode_root_dir=C:\Users\Administrator\.node-gyp\6.11.4',

    gyp info spawn args '-Dnode_gyp_dir=C:\Users\Administrator\AppData\Roaming\npm\node_modules\node-gyp',

    gyp info spawn args '-Dnode_lib_file=C:\Users\Administrator\.node-gyp\6.11.4\<(target_arch)\node.lib',

    gyp info spawn args '-Dmodule_root_dir=C:\SourceCode\HenryProjects\HenryNodeTest\CppAddons\demo',

    gyp info spawn args '-Dnode_engine=v8',

    gyp info spawn args '--depth=.',

    gyp info spawn args '--no-parallel',

    gyp info spawn args '--generator-output',

    gyp info spawn args 'C:\SourceCode\HenryProjects\HenryNodeTest\CppAddons\demo\build',

    gyp info spawn args '-Goutput_dir=.' ]

    Traceback (most recent call last):

    File "C:UsersAdministratorAppDataRoaming pm ode_modules ode-gypgypgyp_main.py", line 16, in <module>

    sys.exit(gyp.script_main())

    File "C:UsersAdministratorAppDataRoaming pm ode_modules ode-gypgyppylibgyp\__init__.py", line 545, in script

    _main

    return main(sys.argv[1:])

    File "C:UsersAdministratorAppDataRoaming pm ode_modules ode-gypgyppylibgyp\__init__.py", line 538, in main

    return gyp_main(args)

    File "C:UsersAdministratorAppDataRoaming pm ode_modules ode-gypgyppylibgyp\__init__.py", line 514, in gyp_ma

    in

    options.duplicate_basename_check)

    File "C:UsersAdministratorAppDataRoaming pm ode_modules ode-gypgyppylibgyp\__init__.py", line 130, in Load

    params['parallel'], params['root_targets'])

    File "C:UsersAdministratorAppDataRoaming pm ode_modules ode-gypgyppylibgypinput.py", line 2778, in Load

    variables, includes, depth, check, True)

    File "C:UsersAdministratorAppDataRoaming pm ode_modules ode-gypgyppylibgypinput.py", line 394, in LoadTarge

    tBuildFile

    includes, True, check)

    File "C:UsersAdministratorAppDataRoaming pm ode_modules ode-gypgyppylibgypinput.py", line 246, in LoadOneBu

    ildFile

    None)

    File "binding.gyp", line 4

    target_name addon,

    ^

    SyntaxError: invalid syntax

    gyp ERR! configure error

    gyp ERR! stack Error: `gyp` failed with exit code: 1

    gyp ERR! stack at ChildProcess.onCpExit (C:UsersAdministratorAppDataRoaming pm ode_modules ode-gyplibconfig

    ure.js:336:16)

    gyp ERR! stack at emitTwo (events.js:106:13)

    gyp ERR! stack at ChildProcess.emit (events.js:191:7)

    gyp ERR! stack at Process.ChildProcess._handle.onexit (internal/child_process.js:219:12)

    gyp ERR! System Windows_NT 6.1.7601

    gyp ERR! command "C:\Program Files\nodejs\node.exe" "C:\Users\Administrator\AppData\Roaming\npm\node_modules\n

    ode-gyp\bin\node-gyp.js" "configure"

    gyp ERR! cwd C:SourceCodeHenryProjectsHenryNodeTestCppAddonsdemo

    gyp ERR! node -v v6.11.4

    gyp ERR! node-gyp -v v3.6.2

    gyp ERR! not ok

     

    错误说的是binding.gyp的语法不对。

    仔细看了一下,这个文件缺失有问题,怎么标点符号都没有了!

     

    于是重新从官网的例子里面拷贝一份内容。

    再次运行,这次成功了!真不容易啊。

    查看物理路径,发现生成了一个build文件夹。

    打开文件夹,里面是个c++的解决方案。

     

    1. 用VS2013打开这个解决方案。是这样的一个结构:

       

      编译一下,报这个错误:

       

      查了一下,build tool v141是VS2015. 这个麻烦了,我只有VS2013.

      查了一下,解决方法是改一下项目的配置。

      在上面的选择框中选择Visual Studio 2013(v120),也就是第一个。

      然后再次编译,成功了!

       

      打开bin/Debug目录,addon.node赫然在目。

       

    2. 现在来测试这个c++ addon.

      创建一个testdemoaddon.js文件。里面按照官方的例子写如下的代码:

    // hello.js
    const addon = require('./build/Release/addon');

     

    console.log(addon.hello());
    // Prints: 'world'

     

    这里的引用路径需要改成你自己机器上的相对路径。比如Release可能需要改成Debug.

    然后命令行定位到测试文件所在的路径。然后运行:

    node testdemoaddon.js

     

    结果如下:

     

    至此为止,用VS2013创建的第一个Node.js的Addon成功运行。

    如果想继续写更复杂的Addon, 就需要继续按照这个文章来学习了:

    https://nodejs.org/api/addons.html#addons_hello_world

     

     

     

     

     

     

  • 相关阅读:
    POJ1486 Sorting Slides 二分图or贪心
    POJ2060 Taxi Cab Scheme 最小路径覆盖
    POJ3083 Children of the Candy Corn 解题报告
    以前的文章
    POJ2449 Remmarguts' Date K短路经典题
    这一年的acm路
    POJ3014 Asteroids 最小点覆盖
    POJ2594 Treasure Exploration 最小路径覆盖
    POJ3009 Curling 2.0 解题报告
    POJ2226 Muddy Fields 最小点集覆盖
  • 原文地址:https://www.cnblogs.com/time-is-life/p/8723694.html
Copyright © 2011-2022 走看看