将变量从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/