问题
I am having trouble with this error I am receiving whenever I run my application. The error is:
loop or previous error loading module 'socket'
.
The code that is causing this error is:
socket = require("socket")
.
This error occurs during the first lua_pcall
. Here is the function that calls that:
void startTerminal(int port, char host[80])
{
lua_State *L = lua_open();
/* Open Lua Library */
luaL_openlibs(L);
/* Choose the lua file that will run */
if(luaL_loadfile(L, "socket.lua")) {
lfatal(L, "luaL_loadfile() failed");
}
/* Start lua file */
if(lua_pcall(L, 0, 0, 0)) {
lfatal(L, "lua_pcall()");
}
/* Get connect function */
lua_getglobal(L, "connect");
if(!lua_isfunction(L, -1)) {
lua_pop(L, 1);
lfatal(L, "lua_isfunction() failed");
}
/* Setup arguments */
lua_pushnumber(L, port);
lua_pushstring(L, host);
/* Call the lua function */
if(lua_pcall(L, 2, 2, 0)) {
lfatal(L, "lua_pcall() failed");
}
/* Print out results */
printf("%s", lua_tostring(L, -1));
printf("%s", lua_tostring(L, -1));
lua_close(L);
}
Here is how I am compiling the code:
gcc -Wall -o terminal attacker.c -I/usr/include/lua5.1 -llua5.1 -lm
Am I missing any switches during compile or am I missing library?
NOTE:
The compiler does not throw any errors and compiles cleanly.
In other Lua applications that does not include C, I don't have any problem with require("socket")
.
Thanks
回答1:
luaL_loadfile(L, "socket.lua")
This is suspect. Very suspect.
Using the standard Lua loaders, when you issue require("MODULE_NAME")
, the very first thing it will look for (after checking to see if MODULE_NAME
was already loaded) will be "MODULE_NAME.lua". In the current directory. Which certainly exists. It's called socket.lua
, the very file you've loaded and are trying to execute. Therefore, it's going to try to load socket.lua
as a module.
And since socket.lua
has require("socket")
in it, it will load itself again. And again. And again.
Well, it won't because Lua's package loader system is smart enough to detect loops and issue an error. Which is exactly what it did.
So don't name a file MODULE_NAME.lua
if you actually are going to require a module with that name.
来源:https://stackoverflow.com/questions/12700342/error-loading-module-lua