从 NodeJS 到 C++ 我应该如何传递参数并获得结果

2 c++ node.js node.js-addon

我正在尝试将我的旧 C++ 项目与 NodeJS 集成。我看过 hello word 插件教程将简单的文件 c++ 文件构建到 NodeJS 中。我正在寻找一些高级示例,如果有人致力于构建从 NodeJS 到 C++ 的桥梁。我想将数据从我的 nodeJS 程序(hello.js)传递到 C++ 程序(hello.cc)

感谢帮助

小智 5

您需要安装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" ]
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

现在在这个 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 + '}');
Run Code Online (Sandbox Code Playgroud)

有一篇不错的简短文章 http://www.benfarrell.com/2013/01/03/c-and-node-js-an-unholy-combination-but-oh-so-right/