Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call C code from node.js

Tags:

c++

c

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

for example bus-driver.h

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

I want to call these functions from node.js.

like image 397
Nasr Avatar asked Nov 19 '25 07:11

Nasr


1 Answers

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.

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

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

Then in your javascript do something similar to this:

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.

like image 142
dfranca Avatar answered Nov 20 '25 19:11

dfranca



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!