c++ - 以高效的方式从 Node.js 运行 C 或 C++ 代码

标签 c++ c node.js requirejs

假设我有一个简单的 Hello, World! 用 C++ 或 C 编写的文件(任何能帮助我在 Node.js 中更轻松地使用它的东西,最好是 C),并希望从 Node.js 文件运行它。考虑到该文件将用于提高性能(将 CPU 密集型函数从 Node.js 更改为 C/C++),最有效的方法是什么?

我遇到了 the addons ,但在我看来,为了使用它,我必须转换大量代码才能将其转换为该格式。有没有更简单的方法?

最佳答案

我不明白为什么使用 child_process 会比其他选项慢。

我推荐:

// myCFile.c
#include <stdio.h>

int main(void){

    // Processor-intensive computations
    int x = 1+2+3;

    // Send to node via standard output
    printf("%d", x);

    // Terminate the process
    return 0;
}

编译:

gcc -o myExecutable myCFile.c

然后像这样使用child_process:

// File myNodeFile.js
const { exec } = require("child_process");
exec("./myExecutable", (error, stdout, stderr) => console.log(stdout));

关于c++ - 以高效的方式从 Node.js 运行 C 或 C++ 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49100336/

相关文章:

c++ - 你能在 VS 2005 中创建一个 lib 或 dll 并与 VS 2008 链接吗

c++ - cmake - find_library - 自定义库位置

node.js - 我可以安装 node_modules 一次,并且需要所有时间吗

node.js - 在webpack中选择正确的environment.ts

c++ - 在 C++ 中使用 "super"

c++ - boost::asio::async_write 没有完成它的任务

c - Valgrind 可能会在简单程序中丢失内存

c - 内存中连续位置的矩阵

c++ - 用数据点填充矩阵

javascript - 为什么我的 Web 应用程序在发出几个将项目推送到我的 mongodb 数据库的请求后暂停?