从node.js调用C代码

[英]Call C code from node.js


Let's say i have source and header files for C code (bus-driver.c and bus-driver.h) can i call functions inside them from node.js

假设我有C代码的源代码和头文件(bus-driver.c和bus-driver.h)我可以从node.js调用它们内部的函数

for example bus-driver.h

例如bus-driver.h

void bus_init(void);
void bus_write(char *buf);

I want to call these functions from node.js.

我想从node.js调用这些函数。

1 个解决方案

#1


3  

The nodeffi seems to be simplest way to do that. I didn't test it so it can has problems that I don't realize now.

nodeffi似乎是最简单的方法。我没有测试它,所以它可能有我现在没有意识到的问题。

But I would suggest to do something like that, following the tutorial. Install nodeffi:

但我建议按照教程进行类似的操作。安装nodeffi:

Generate a library for your bus-driver if you don't have one, let's call it libbusdriver.

如果没有总线驱动程序,请为您的总线驱动程序生成一个库,我们称之为libbusdriver。

Then in your javascript do something similar to this:

然后在你的javascript做类似的事情:

var ffi = require('ffi');

var libbusdriver = ffi.Library('libbusdriver', {
  'bus_init': [ 'void', [ 'void' ] ],
  'bus_write': [ 'void', [ 'string' ] ],
});
libbusdriver.bus_init();
libbusdriver.bus_write("Hello");

Let me know if it helps.

如果有帮助,请告诉我。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/08/25/bfa6e8ee2b230e2965dddc520f5e4589.html



 
  © 2014-2022 ITdaan.com 联系我们: