I'm making a Reversi game and I coded my bot algorithm in C. However, I want to create a web based GUI and somehow be able to call my algorithm written in C through the web app (I don't want to recode my bot using JavaScript).
I've searched around online and I think that this problem has to do something related to APIs, but I don't really know what to do or where to start. Any help?
Assuming that you already have an back-end for the webapp, you can compile the C code into a DLL (in windows) or an SO (in Unix based systems) file and then call it in the backend code.
For example creating an SO file
gcc -c -fPIC myfile.c -o myfile.o
gcc -shared myfile.o -o libmyfile.so
We are compiling the code into an object file and linking it to an SO file. The -fPIC flag specifies position-independent code, which is required for creating shared objects.
Note that the name convention for shared objects is often to prefix them with "lib" and use the extension .so.
Once you have the DLL or SO file, you can load it within your backend server code and call the functions defined in your C algorithm. The exact method for loading the library will depend on the programming language and framework you choose for your backend.
To give an example in Node JS server,
npm install ffi
const ffi = require('ffi');
// Load the shared object file
const myLibrary = ffi.Library('/path/to/myLibrary.so', {
'myFunction': ['int', ['int', 'int']]
});
const result = myLibrary.myFunction(3, 4);
console.log(result);
In this example, myFunction is the function defined in the shared object file myLibrary.so. The function takes two integer arguments and returns an integer result. You can adjust the function signature and data types according to your specific shared object file.
hope it helps.