從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 联系我们: