首先看我的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:\Users\Administrator\AppData\Roaming\npm\node_modules\node-gyp\lib\configur

e.js:483:19)

gyp ERR! stack at Object.<anonymous> (C:\Users\Administrator\AppData\Roaming\npm\node_modules\node-gyp\lib\configure

.js:508:16)

gyp ERR! stack at C:\Users\Administrator\AppData\Roaming\npm\node_modules\node-gyp\node_modules\graceful-fs\polyfill

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:\SourceCode\HenryProjects\HenryNodeTest\CppAddons\demo

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:\Python\Python36", you can set the PYTHON env variable.

gyp ERR! stack at Object.failNoPython (C:\Users\Administrator\AppData\Roaming\npm\node_modules\node-gyp\lib\configur

e.js:483:19)

gyp ERR! stack at Object.<anonymous> (C:\Users\Administrator\AppData\Roaming\npm\node_modules\node-gyp\lib\configure

.js:508:16)

gyp ERR! stack at C:\Users\Administrator\AppData\Roaming\npm\node_modules\node-gyp\node_modules\graceful-fs\polyfill

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:\SourceCode\HenryProjects\HenryNodeTest\CppAddons\demo

gyp ERR! node -v v6.11.4

gyp ERR! node-gyp -v v3.6.2

gyp ERR! not ok

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

看错误提示是在C:\Python\Python36目录下找不到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:\Python\Python36\python.EXE", you can set the PYTHON env variable.

gyp ERR! stack at Object.failNoPython (C:\Users\Administrator\AppData\Roaming\npm\node_modules\node-gyp\lib\configur

e.js:483:19)

gyp ERR! stack at Object.<anonymous> (C:\Users\Administrator\AppData\Roaming\npm\node_modules\node-gyp\lib\configure

.js:508:16)

gyp ERR! stack at C:\Users\Administrator\AppData\Roaming\npm\node_modules\node-gyp\node_modules\graceful-fs\polyfill

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:\SourceCode\HenryProjects\HenryNodeTest\CppAddons\demo

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:\Python\Python36", you can set the PYTHON env variable.

gyp ERR! stack at Object.failNoPython (C:\Users\Administrator\AppData\Roaming\npm\node_modules\node-gyp\lib\configur

e.js:483:19)

gyp ERR! stack at Object.<anonymous> (C:\Users\Administrator\AppData\Roaming\npm\node_modules\node-gyp\lib\configure

.js:508:16)

gyp ERR! stack at C:\Users\Administrator\AppData\Roaming\npm\node_modules\node-gyp\node_modules\graceful-fs\polyfill

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:\SourceCode\HenryProjects\HenryNodeTest\CppAddons\demo

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:\Python27\python.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:\Users\Administrator\AppData\Roaming\npm\node_modules\node-gyp\gyp\gyp_main.py", line 16, in <module>

sys.exit(gyp.script_main())

File "C:\Users\Administrator\AppData\Roaming\npm\node_modules\node-gyp\gyp\pylib\gyp\__init__.py", line 545, in script

_main

return main(sys.argv[1:])

File "C:\Users\Administrator\AppData\Roaming\npm\node_modules\node-gyp\gyp\pylib\gyp\__init__.py", line 538, in main

return gyp_main(args)

File "C:\Users\Administrator\AppData\Roaming\npm\node_modules\node-gyp\gyp\pylib\gyp\__init__.py", line 514, in gyp_ma

in

options.duplicate_basename_check)

File "C:\Users\Administrator\AppData\Roaming\npm\node_modules\node-gyp\gyp\pylib\gyp\__init__.py", line 130, in Load

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

File "C:\Users\Administrator\AppData\Roaming\npm\node_modules\node-gyp\gyp\pylib\gyp\input.py", line 2778, in Load

variables, includes, depth, check, True)

File "C:\Users\Administrator\AppData\Roaming\npm\node_modules\node-gyp\gyp\pylib\gyp\input.py", line 394, in LoadTarge

tBuildFile

includes, True, check)

File "C:\Users\Administrator\AppData\Roaming\npm\node_modules\node-gyp\gyp\pylib\gyp\input.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:\Users\Administrator\AppData\Roaming\npm\node_modules\node-gyp\lib\config

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:\SourceCode\HenryProjects\HenryNodeTest\CppAddons\demo

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

VS2013创建Node.js C++ Addons的过程的更多相关文章

  1. 用http-server 创建node.js 静态服务器

    今天做一本书上的例子,结果代码不能正常运行,查询了一下,是语法过时了,书其实是新买的,出版不久. 过时代码如下 var connect=require('connect'); connect.crea ...

  2. 创建Node.js TypeScript后端项目

    1.安装Node.js扩展,支持TypeScript语法 npm install -g typescript   npm install -g typings 2.创建项目目录project_fold ...

  3. npm学习(六)之如何创建 Node.js 模块

    如何创建 Node.js 模块 Node.js 模块是一种可以发布到 npm 的包.当你创建一个新模块时,创建 package.json 文件是第一步. 你可以使用 npm init 命令创建 pac ...

  4. 用vs2013开发node.js的addon.

        下载node.js的源代码. https://github.com/joyent/node 如果用svn下载,后面加上/trunk,以免把用不着的branches也下载下来,浪费时间. 安装V ...

  5. 总结在Visual Studio Code创建Node.js+Express+handlebars项目

    一.安装node.js环境. Node.js安装包及源码下载地址为:https://nodejs.org/en/download/ 32 位安装包下载地址 : https://nodejs.org/d ...

  6. 在 Web 应用中创建 Node.js 应用程序

    本分步指南将通过 Azure Web 应用帮助您启动并运行示例 Node.JS 应用程序.除 Node.JS 外,Azure Web 应用还支持其他语言,如 PHP..NET.Node.JS.Pyth ...

  7. 创建node.js一个简单的应用实例

    在node.exe所在目录下,创建一个叫 server.js 的文件,并写入以下代码: //使用 require 指令来载入 http 模块 var http = require("http ...

  8. 在vs2017中创建Node.js项目

    https://github.com/Microsoft/nodejstools/wiki/Projects 1. 安装vs2017的时候,需要勾选Node.js 2.新建项目的时候,选择其他语言,然 ...

  9. 【每天学一点-02】创建Node.js的第一个应用

    1.引入require模块,使用createServer()创建服务器 [server.js]文件 var http = require('http'); http.createServer(func ...

随机推荐

  1. 使用CefSharp在.Net程序中嵌入Chrome浏览器(十)——独立文件夹部署

    CefSharp本身携带了一大堆文件,这些文件默认直接释放在exe文件底下,这种方式本身没有什么问题,但多了一大堆文件后不是很好看.本文这里就介绍一个方法,使得可以将CEF相关的文件部署到独立的文件夹 ...

  2. What is CMSIS-DAP

    The mbed HDK and mbed-enabled hardware support the CMSIS-DAP debug interface, which consists of an a ...

  3. [译] Go 并发编程基础

    原文:Fundamentals of concurrent programming 译者:youngsterxyf 本文是一篇并发编程方面的入门文章,以Go语言编写示例代码,内容涵盖: 运行期并发线程 ...

  4. 基于开源SuperSocket实现客户端和服务端通信项目实战

    一.课程介绍 本期带给大家分享的是基于SuperSocket的项目实战,阿笨在实际工作中遇到的真实业务场景,请跟随阿笨的视角去如何实现打通B/S与C/S网络通讯,如果您对本期的<基于开源Supe ...

  5. Delphi模拟最小化恢复关闭按纽

    https://yq.aliyun.com/wenji/96083 本文讲的是Delphi模拟最小化恢复关闭按纽, 我们做多文档应用程序开发时,如果在主From中指定mainMenu时,在主菜单上右角 ...

  6. TMS WEB CORE直接从HTML&CSS设计的页面布局

    TMS WEB CORE直接从HTML&CSS设计的页面布局 TMS WEB CORE支持DELPHI IDE中拖放控件,生成HTML UI.这种方式适合DELPHI和C++ BUILDER的 ...

  7. 给js创建的一个input数组绑定click事件

    <html> <body> <input type="button" name="input[]" value="按钮1 ...

  8. Python中的文件IO操作(读写文件、追加文件)

    Python中文件的读写包含三个步骤:打开文件,读/写文件,关闭文件. 文件打开之后必须关闭,因为在磁盘上读写文件的功能是由操作系统提供的,文件作为对象,被打开后会占用操作系统的资源,而操作系统在同一 ...

  9. 嘻哈帝国第一季/全集Empire迅雷下载

    英文译名Empire,第1季(2015-01-08)FOX.本季看点:<嘻哈帝国>卢西奥斯·莱恩是一名超级音乐明星兼Empire娱乐公司的创始人,故事讲述了他如何在困境和失败中运营公司的故 ...

  10. 用Handler的post()方法来传递线程中的代码段到主线程中执行

    自定义的线程中是不能更新UI的,但是如果遇到更新UI的事情,我们可以用handler的post()方法来将更新UI的方法体,直接传送到主线程中,这样就能直接更新UI了.Handler的post()方法 ...