Search code examples
luaffiluajit

How to call a function in shared library using Lua


I have a shared library where I have some functions. I want to access them by getting their signature using luajit.

I have a libtest_c.so --> /usr/local/lib

lua --> /usr/local/bin

require("test_c")

stdin:1: module 'test_c' not found:

no field package.preload['test_c']
no file './test_c.lua'
no file '/usr/local/share/luajit-2.0.4/test_c.lua'
no file '/usr/local/share/lua/5.1/test_c.lua'
no file '/usr/local/share/lua/5.1/test_c/init.lua'
no file './test_c.so'
no file '/usr/local/lib/lua/5.1/test_c.so'
no file '/usr/local/lib/lua/5.1/loadall.so'

stack traceback: [C]: in function 'require' stdin:1: in main chunk [C]: at 0x00404270


Solution

  • Call a function in shared library using Lua:

    http://luajit.org/ext_ffi.html

    local lib = ffi.load('some.dll')
    ffi.cdef[[
      void hello (void);
    ]]
    lib.hello()