Register C to the lua environment for use

There are two ways to register to lua, one is the lua interpreter, if it supports dynamic linking, use the dynamic linking mechanism to compile the function interface into a dynamic link library, and then put the dynamic link library in the C path (LUA_CPATH) of lua. , and then use require "xxx" directly in the lua file, this sentence will cause the dynamic link library written by the host to link to lua, look for xxx, and register the module in package.loaded. Another way is to directly recompile the interface function written by yourself together with the lua source file to generate a new lua interpreter, and use the new interpreter to replace the old one. Also, there needs to be some way to tell the interpreter that it should open this module at the same time as opening the new state.

Register the interface to the lua environment using dynamic library linking

static luaL_Reg  xxx[] = {
    {"xya", xya},
    {"bbb", bbb},
    {NULL, NULL}
};

int luaopen_xxx(lua_State *L)
{
    /*create module*/
    luaL_register(L, "xxx", xxx);
    return 1;
}

References:

https://blog.csdn.net/nice_xp/article/details/52712140

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324779961&siteId=291194637