从 NodeJS 到 C++ 我应该如何传递参数并获得结果
From NodeJS to C++ how shall I pass arguments and get the result
我正在尝试将我的旧C++项目与NodeJS集成。我已经看过你好单词插件教程,将简单的文件 c++ 文件构建到 NodeJS 中。我正在寻找一些高级示例,如果有人致力于构建从 NodeJS 到 C++ 的桥梁。我想将数据从我的nodeJS程序(hello.js(传递到C++程序(hello.cc(
感谢您的帮助
您需要安装 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/
您可以在以下位置找到使用 NAN 创建本机插件的示例:
- https://github.com/nodejs/node-addon-examples
- https://github.com/nodejs/nan#example
相关文章:
- 使用具有默认参数的函数模板进行 decltype 会使结果混乱(一个有趣的问题或 gcc 的错误)
- 执行参数未提供预期结果
- 术语的计算结果不是采用0个参数的函数
- 错误 C2064:术语的计算结果不是采用 3 个参数的函数
- C++:通过函数参数传递的值给出不同的结果
- 结果失败或多个参数无效
- 使用函数结果作为参数的 C++
- 添加可变参数函数的错误结果
- 从可调用可变参数元组中的函数结果创建元组
- (WMI)ExecMethod out 参数 - 无论调用的结果如何,ResultingSnapshot 都是 NULL
- C++:术语的计算结果不是采用 1 个参数的函数
- 在线程错误 C2064 中:term 的计算结果不为 0 个参数的函数
- 错误 C2064:term 的计算结果不是采用 1 个参数的函数 - 关于线程的一些东西
- 提升线程:术语的计算结果不为 0 个参数
- 当类的对象更改其参数时,如何获得结果
- 是否可以使用自动占位符来推断非类型模板参数中的函数结果
- 为什么在类构造函数方法中,std::string 参数在调试时显示不同的结果?
- 使用 constexpr 函数的结果作为模板参数(clang vs gcc)
- 在 c++ 代码中具有相同的函数和参数的不同结果
- 默认模板参数结果为"expected type-specifier"