错误加载模块(LUA) [英] Error loading module (Lua)

查看:3115
本文介绍了错误加载模块(LUA)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个错误,每当我跑我的应用我收到的麻烦。错误是:
循环或previous错误加载模块'插座'

在code,是造成此错误是:
插座=要求(插座)

在第一个 lua_pcall 出现此错误。下面是调用该函数:

 无效startTerminal(INT端口,焦炭主机[80])
{
    lua_State * L = lua_open();    / *开放的Lua库* /
    luaL_openlibs(L);    / *选择将*运行LUA文件/
    如果(luaL_loadfile(L,socket.lua)){
        lfatal(LluaL_loadfile()失败);
    }    / *启动LUA文件* /
    如果(lua_pcall(L,0,0,0)){
        lfatal(Llua_pcall());
    }    / *获取连接功能* /
    lua_getglobal(L连接);    如果(!lua_isfunction(L,-1)){
        调用lua_pop(L,1);
        lfatal(Llua_isfunction()失败);
    }    / *设置参数* /
    调用lua_pushnumber(L,端口);
    lua_pushstring(L,主机);    / *调用LUA函数* /
    如果(lua_pcall(L,2,2,0)){
        lfatal(Llua_pcall()失败);
    }    / *打印出结果* /
    的printf(%S,lua_tostring(L,-1));
    的printf(%S,lua_tostring(L,-1));    lua_close(L);
}

下面是如何,我编译的是code:

的gcc -o -Wall终端attacker.c -I / usr / include目录/ lua5.1 -llua5.1 -lm

我缺少的编译过程中的任何开关或我缺少库?

请注意:
编译器不抛出任何错误,完全编译。
在其他不包括C Lua的应用程序,我没有与有任何问题需要(插座)

感谢


解决方案

  luaL_loadfile(Lsocket.lua)

这是犯罪嫌疑人。非常令人怀疑的。

使用标准的Lua装载机,当您发出要求(MODULE_NAME),它会寻找(检查后看到的第一件事,如果 MODULE_NAME 已加载)将MODULE_NAME.lua。的在当前目录的。其中肯定存在。这就是所谓的 socket.lua ,你已经加载,并且十分文件试图执行。因此,它会试图加载 socket.lua 作为一个模块。

和自 socket.lua 要求(插座)中,它会再次加载自身。然后再次。又一遍。

那么,它不会因为Lua的包装载系统非常智能,能够检测循环,并发出一个错误。这正是它没有。

所以,不要命名文件 MODULE_NAME.lua 如果你真的打算要求该名称的模块。

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

解决方案

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.

这篇关于错误加载模块(LUA)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆