In project development, we sometimes need to call some library files or dll files written in C language. At this time, we can use nodejs to call the C method to achieve this.
In order to use nodejs to call c library files, we need to first understand how to write c library files. Here we take writing a simple addition library file as an example.
First, we need to create a c fileadd.c
. The content of the file is as follows:
#include <stdio.h> int add(int a, int b) { printf("c add function has been called "); return a + b; }
This c file contains only A function add
is defined that receives two integer parameters and returns their sum.
Next, we need to use a compiler to compile the c file into a dynamic link library or a static link library. Here we take the gcc compiler under Linux system as an example.
The dynamic link library compilation command is as follows:
$ gcc -fPIC -shared -o libadd.so add.c
The static link library compilation command is as follows:
$ gcc -c add.c $ ar rcs libadd.a add.o
The -fPIC
parameter here is to let the compiler generate Position-independent code so that code segments can be shared correctly across different process address spaces. The generated dynamic link library or static link library files are libadd.so
and libadd.a
respectively.
After we have the c library file, we can call the function in this library file in nodejs. Here we take nodejs under Linux system as an example.
The ffi module of Node.js can use external dynamic libraries, static libraries and C library functions. But in new Node.js versions, this module may have issues. If we need to use the more stable ffi module, we can choose to use the ffi-napi module to call the C library in the process.
We can use the following command to install the ffi-napi module in the project:
$ npm install ffi-napi
Next, we need to create a nodejs file app.js
, the file content is as follows:
const ffi = require('ffi-napi'); const libadd = ffi.Library('./libadd', { 'add': ['int', ['int', 'int']] }); console.log(libadd.add(1, 2));
It should be noted here that the first parameter of the Library()
function is the path to the c library file, and the file name under different platforms The suffix may also be different. The second parameter is an object containing the name and parameter type of the function to be imported. Here we import the add
function and specify its parameter type and return value type.
Finally we called the add
function and printed out the return result.
We can enter the following command in the command line to run the nodejs program:
$ node app.js
The output results are as follows:
c add function has been called 3
Using nodejs to call c library files can greatly improve project development efficiency, and also provides nodejs developers with more development possibilities. The basic steps can be summarized as:
Using nodejs to call c library files can improve the performance and stability of the application and help us better complete project development.
The above is the detailed content of A simple way to call c using nodejs. For more information, please follow other related articles on the PHP Chinese website!