Best way for Lua script to call a C shared lib?

前端 未结 2 1774
礼貌的吻别
礼貌的吻别 2021-01-13 01:36

I come from more of a Python and CTYPES background...and am trying to work out the best way to call a standard C shared lib from Lua script.

I have heard of \"Alien\

相关标签:
2条回答
  • 2021-01-13 01:53

    Lua cannot call C libraries out of the box. It does not ship with libffi, and as such doesn't work like ctypes.

    Historically, lua is embedded into an application which in turn will add to the lua tables the needed functions and provide the lua stack manipulation to pass and return parameters.

    Alien is a libffi adaptation and will work.

    0 讨论(0)
  • 2021-01-13 02:09

    Like Yann said, Lua cannot call C libraries by default. The usual practice is to expose the wanted API by means of creating a binding library. There are several places, where you can look for existing library bindings for Lua, like the wiki, LuaRocks or LuaDist.

    If no bindings exist, you may try to use Alien to bind straight to C libraries using libffi.

    For your example - Sqlite3, there are at least 2 bindings:

    • generic SQL binding - LuaSql, provides binding to Sqlite3, MySQL, Oracle...
    • specialized binding - Lua-Sqlite3
    0 讨论(0)
提交回复
热议问题