call c function in lua

Code in here.

This project use Lua5.2.4, visual studio 2017. You can find the lua binary file located in TestLua/luafolder.

To create a C library and export function to lua. Follow this steps:

  • Define your function with following signature

    1
    typedef int (*lua_CFunction)(lua_State* L)

    example

    1
    int myadd(lua_State* L);
  • Construct a luaL_Reg array

    1
    2
    3
    4
    const struct luaL_Reg mylib[] ={
    {"myadd", myadd},
    {NULL, NULL}
    }

    and luaL_Reg is defined in lauxlib.h

    1
    2
    3
    4
    typedef struct luaL_Reg {
    const char *name;
    lua_CFunction func;
    } luaL_Reg;

So the first is the function name which will be used in lua, and the second is the function address in c library. Note that the last element in this array must be {NULL, NULL}.

  • Define a luaopen_DLLName function

    1
    2
    3
    4
    int luaopen_TestLua(lua_State *L){
    luaL_newlib(L, mylib);
    return 1;
    }

    This function name should be match pattern luaopen_dllname, dllname is your dll’s name

  • Final step, Use this c library in your lua code.

    1
    2
    3
    4
    -- test.lua

    local t = require "TestLua"
    print(t.myadd(1, 2))

    when you run this test.lua script in lua environment, it should output correct answer. Remerber to use same lua exe version to run this script.

    Note that the module name is case sensitive because internally lua interpreter will call luaopen_Modulename function which exported by your c library.

Another note, you should export luaopen_Modulename method and use extern "C"to modifier all function to make it follow the C standard if you are using c++.