So here is an example how one can use the luajit ffi to use external c functions, in this case self written. I'm obviously on linux btw.
In the bot *.lua file I have
Code:
ffi.cdef[[
void setVectorZero(double* vec, int nn);
]]
local hl = ffi.load("./bots/myBots/helperlib.so")
local cvec = ffi.new("double[?]", 100)
helpers.setVectorZero(cvec, 100)
Note that the path in ffi.load is relative to the berrybot executable.
The helperlib.so is a self written dynamic library from the c file
Code:
void setVectorZero(double* vec, int nn) {
for (int ii = 0; ii < nn; ii++) {
vec[ii] = 0.;
}
}
which was just compiled by "gcc -fPIC -shared -o helperlib.so helperlib.c". It's obviously just a stupid example, but anyway.
Some notes:
FFI is enabled by berrybots by default, which I do like, since doing some stuff in C is helpful, even when we're not talking about using external libraries.
FFI can be used to load any dynamic library on the system. Which I personally like as well, even if only for testing stuff with existing libraries inside berrybot (e.g. kdtrees or something).
BUT the problem is that my understanding is that berrybots tries to be "secure" in that regard. I don't see any restrictions on loading iostream for example and manipulating files on the disk. Whether one compiles the shared library oneself or not doesn't really matter, at least if one compiles the helper library oneself, then no other libraries have to be linked, but as I said that doesn't include iostream...
It's already pretty late here, but I just wanted to write stuff down so I don't forget.
Cheers