javascript - 使用 NodeJS 中的变量启动 C 程序

标签 javascript c node.js

将变量从nodeJS发送到C程序(不是C++)的最简单方法是什么?并在收到变量后运行这个 C 程序?

app.js:

 var test = 1;

测试.c:

#include <stdio.h>     
int main()
{
  int node_variable;
  printf("Value from nodeJS is %d", node_variable);

  return 0;
}

最佳答案

您可以使用 nodejs child_process 模块将参数传递给 C 程序(例如,请参阅 here)。

app.js:

var test = 1;
var exec = require('child_process').exec;
exec('./test.bin '+test, function callback(error, stdout, stderr){console.log(stdout);});

测试.c:

#include <stdio.h>

int main(int argc, char **argv) {

  printf("value of test: %s\n", argv[1]);
  return 0;
}

假设test.bin是由test.c构建的程序,执行javascript文件使编译后的程序显示test的值(此处为“1”) 。请注意,变量 test 的值被视为单个(非空)参数。

关于javascript - 使用 NodeJS 中的变量启动 C 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42003436/

相关文章:

python - 使用 Cython 和 "next"编译

android - 将字符串从 Java 传递到 C++ 并使用带有 NDK 案例错误的 popen?

c++ - 如何计算16bit到8bit的量化误差?

javascript - Jquery 中两个选择框具有一个功能

javascript - 如何在使用javascript创建表格时创建复选框?

javascript - CSS 缩放属性

Node.JS Express 4 - Mongoose 不保存数据

javascript - 如何访问变体中的颜色?在 Vue 中

node.js - 覆盖 ember 中的 Node 包

javascript - 创建多个ftp客户端分别从不同的服务器获取文件。这在 Node 中可能吗?