Retrieving a module object in Lua

I have a C program that uses Lua to run some scripts. I need to open Lua libraries using C code, for example luaopen_socket_core(myLuaState)

, for some reason I cannot load modules from Lua code, for example socket = require "luasocket"

.

Once I got the idea of ​​this program, I need to load a library called struct , so I added struct.c

to my project, and when I tried to use its functions like struct.unpack

runtimer complains that the global variable is not called struct

. Of course it was loaded luaopen_struct(myLuaState)

instead of struct = require "struct"

which I was banned from.

Is there any suggestion about the possibility of using this variable struct

?

+3


source to share


1 answer


Take a look at luaL_requiref

the helper library that simulates require

being called from Lua.



You probably called a public function right away and forgot to set these variables manually, this function will do it all for you.

+3


source







All Articles