从 NodeJS 到 C++ 我应该如何传递参数并获得结果
From NodeJS to C++ how shall I pass arguments and get the result
我正在尝试将我的旧 C++ 项目与 NodeJS 集成。我看过 hello word 插件教程,用于将简单文件 c++ 文件构建到 NodeJS 中。我正在寻找一些高级示例,如果有人致力于构建从 NodeJS 到 C++ 的桥梁。我想将数据从我的 nodeJS 程序 (hello.js) 传递到 C++ 程序 (hello.cc)
感谢帮助
您可以在以下位置找到使用 NAN 创建原生插件的示例:
您需要安装 node-gyp
- 您可能已经安装了
npm install -g node-gyp
有一个很好的例子请看一看
https://gist.githubusercontent.com/bengfarrell/4440739/raw/56e8291a31eb8f9714f8ca975c1e78a0788ae018/randomcoords.cpp
如果您无法访问,请告诉我,我会把它传到这里
到运行这个例子
像往常一样,您需要构建它
然后创建一个 binding.gyp 文件
{
"targets": [
{
"target_name": "randomcoords",
"sources": [ "randomcoords.cc" ]
}
]
}
现在在这个 c++ 插件中你可以像这样传递参数
var randCoords = require("./libs/build/Release/randomcoords.node");
var cursor = randCoords.getRandomCoords3D(600, 400, 100); // params are max values for random output
console.log('{ "x":' + cursor.x + ', "y":' + cursor.y + ', "z":' + cursor.z + '}');
有一篇不错的短文
http://www.benfarrell.com/2013/01/03/c-and-node-js-an-unholy-combination-but-oh-so-right/
我正在尝试将我的旧 C++ 项目与 NodeJS 集成。我看过 hello word 插件教程,用于将简单文件 c++ 文件构建到 NodeJS 中。我正在寻找一些高级示例,如果有人致力于构建从 NodeJS 到 C++ 的桥梁。我想将数据从我的 nodeJS 程序 (hello.js) 传递到 C++ 程序 (hello.cc)
感谢帮助
您可以在以下位置找到使用 NAN 创建原生插件的示例:
您需要安装 node-gyp
- 您可能已经安装了
npm install -g node-gyp
有一个很好的例子请看一看 https://gist.githubusercontent.com/bengfarrell/4440739/raw/56e8291a31eb8f9714f8ca975c1e78a0788ae018/randomcoords.cpp
如果您无法访问,请告诉我,我会把它传到这里
到运行这个例子 像往常一样,您需要构建它 然后创建一个 binding.gyp 文件
{
"targets": [
{
"target_name": "randomcoords",
"sources": [ "randomcoords.cc" ]
}
]
}
现在在这个 c++ 插件中你可以像这样传递参数
var randCoords = require("./libs/build/Release/randomcoords.node");
var cursor = randCoords.getRandomCoords3D(600, 400, 100); // params are max values for random output
console.log('{ "x":' + cursor.x + ', "y":' + cursor.y + ', "z":' + cursor.z + '}');
有一篇不错的短文 http://www.benfarrell.com/2013/01/03/c-and-node-js-an-unholy-combination-but-oh-so-right/