node.js c++的扩展

在项目中,使用webkit做为客户端访问web服务器,webkit须要访问本地资源,node.js没有提供相应的支持,因此准备本身写一个用c++做为编程语言的node.js扩展,现将最简单的helloworld实现过程记录以下:node

须要安装的软件:Git-2.5.1-32-bit.exe   python-2.7.11.msi  node-v4.3.1-x86.msi   vs.net 或 msbuild python

1.到github网站下载,示例源码 https://github.com/lupomontero/node-cpphelloc++

在DOS提示符下执行下面的命令git

# Install node-gyp globally if not installed yet
npm install -g node-gyp

# Clone, build and test git clone git://github.com/lupomontero/node-cpphello.git cd node-cpphello node-gyp configure build npm test

在执行node-gyp configure 时,须要用到msbuild2012,个人电脑安装的vs2015,出错,提示,找不到cpphello.vcxproj文件中的第19行,找不到定义的$(VCTargetsPath)\Microsoft.Cpp.Default.props文件,经分析
$(VCTargetsPath)定义的路径不对,在注册表时查找 VCTargetsPath 的定义,在HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\ToolsVersions\14.0找到$(VCTargetsPath)的定义项,可能好几个,改为$([MSBuild]::ValueOrDefault('$(VCTargetsPath)','$(MSBuildExtensionsPath32)\Microsoft.Cpp\v4.0\V140\')),重点是140(vs2015),其它的版本做相应的修改。
在当前目录下生成./build/Release/cpphello.node

2.调用cpphello.node
与一个test.js文件,内容为

var cpphello = require('./build/Release/cpphello');
console.log(cpphello.foo());github

好了,在dos黑窗口里调用,试试吧  node test.jsweb

相关文章
相关标签/搜索