[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: wrapping c lib
- From: Philipp Janda <siffiejoe@...>
- Date: Sat, 18 Jul 2015 11:34:42 +0200
Am 17.07.2015 um 16:30 schröbte Gregg Reynolds:
Hi list,
Hi!
I'm just starting on a lua wrapper of libmraa for the intel Edison (
http://iotdk.intel.com/docs/master/mraa/). I've got a basic implementation
working. My question is whether it is possible to split the code into
multiple c files.
Sure it is.
libmraa is split into seven modules so I'd like to keep
the wrapper code distinct. But my understanding is that the interface
functions must be static.
C functions exported to Lua don't *have* to be static. Lua uses them by
pointer only, so they usually *can* be static, and it's good practice to
limit symbol visibility as much as possible. But nobody prevents you
from using non-static functions if you want to reference those symbols
from another file (e.g. to put them into a `luaL_Reg` array). I usually
don't do that though, because it gets inconvenient when the number of
exported functions is large. Instead, I do something like the following:
LUAMRAA_API int luaopen_luamraa( lua_State* L ) {
lua_newtable( L );
luamraa_mod1_setup( L );
luamraa_mod2_setup( L );
luamraa_mod3_setup( L );
return 1;
}
The `luamraa_modX_setup` functions are defined in separate files and
register all functions, metatables, etc. in that file. They can't be
`static`, but they don't have to be exported from the shared library
either (on Unices you can have hidden visibility, on Windows you don't
need `__declspec(dllexport)` stuff).
I can create seven wrappers but I'd like to have
one "luamraa" module as well. Is the Right Way to do this to just use a
lua module to import the seven wrappers? (My lua is a little rusty,
haven't worked with it for several years.)
You can also use separate modules per C file and collect those modules
in a single shared object/DLL (e.g. module `luamraa.modX` defined in
function `luaopen_luamraa_modX` exported from DLL `luamraa.dll`). This
makes sense if the modules are more or less independent from each other.
For convenience you may load sub-modules from the "main" module using
`require` in Lua or `luaL_requiref` in C. There are also some modules
out there that do some form of auto-loading via metatables ...
Thanks,
Gregg
HTH,
Philipp