【问题标题】:Require Lua Module In Script Called from C++在从 C++ 调用的脚本中需要 Lua 模块
【发布时间】:2017-07-22 04:31:26
【问题描述】:

我在带有 Lua5.1 的 C++ 应用程序中使用 VS2015。我正在运行一个非常简单的 Lua 脚本,没有问题,原始 lua 工作正常。但是当我尝试导入一个lua模块“socket.http”时,我的应用程序不喜欢它,因为我想它找不到模块。

我的问题是如何让我的 lua 脚本(从 c++ 运行)访问 lua 模块,如 socket.http?

我的项目.cpp

#include "stdafx.h"
#include <iostream>

extern "C"
{
#include <../dependancies/lua51/include/lua.h>
#include <../dependancies/lua51/include/lauxlib.h>
#include <../dependancies/lua51/include/lualib.h>
}

void report_errors(lua_State *L, int status)
{
    if (status != 0)
    {
        printf("-- %s\n", lua_tostring(L, -1));
        lua_pop(L, 1); // remove error message
    }
}

int main()
{
    // create a Lua state
    lua_State* L = luaL_newstate();

    // load standard libs 
    luaL_openlibs(L);

    int lscript = luaL_dofile(L, "test1.lua");

    report_errors(L, lscript);

    system("PAUSE");
    return 0;
}

test1.lua

local http = require("socket.http")

错误

 module 'socket.http' not found:
    no field package.preload['socket.http']
    no file '.\socket\http.lua'
    no file 'C:\Users\georg\Desktop\git\ARGO\Game\ATracknophilia\Debug\lua\socket\http.lua'
    no file 'C:\Users\georg\Desktop\git\ARGO\Game\ATracknophilia\Debug\lua\socket\http\init.lua'
    no file 'C:\Users\georg\Desktop\git\ARGO\Game\ATracknophilia\Debug\socket\http.lua'
    no file 'C:\Users\georg\Desktop\git\ARGO\Game\ATracknophilia\Debug\socket\http\init.lua'
    no file 'C:\Program Files (x86)\Lua\5.1\lua\socket\http.luac'
    no file '.\socket\http.dll'
    no file '.\socket\http51.dll'
    no file 'C:\Users\georg\Desktop\git\ARGO\Game\ATracknophilia\Debug\socket\http.dll'
    no file 'C:\Users\georg\Desktop\git\ARGO\Game\ATracknophilia\Debug\socket\http51.dll'
    no file 'C:\Users\georg\Desktop\git\ARGO\Game\ATracknophilia\Debug\clibs\socket\http.dll'
    no file 'C:\Users\georg\Desktop\git\ARGO\Game\ATracknophilia\Debug\clibs\socket\http51.dll'
    no file 'C:\Users\georg\Desktop\git\ARGO\Game\ATracknophilia\Debug\loadall.dll'
    no file 'C:\Users\georg\Desktop\git\ARGO\Game\ATracknophilia\Debug\clibs\loadall.dll'
    no file '.\socket.dll'
    no file '.\socket51.dll'
    no file 'C:\Users\georg\Desktop\git\ARGO\Game\ATracknophilia\Debug\socket.dll'
    no file 'C:\Users\georg\Desktop\git\ARGO\Game\ATracknophilia\Debug\socket51.dll'
    no file 'C:\Users\georg\Desktop\git\ARGO\Game\ATracknophilia\Debug\clibs\socket.dll'
    no file 'C:\Users\georg\Desktop\git\ARGO\Game\ATracknophilia\Debug\clibs\socket51.dll'
    no file 'C:\Users\georg\Desktop\git\ARGO\Game\ATracknophilia\Debug\loadall.dll'
    no file 'C:\Users\georg\Desktop\git\ARGO\Game\ATracknophilia\Debug\clibs\loadall.dll'

【问题讨论】:

  • socket.http 不是原生 Lua 模块,也许你安装了它?

标签: c++ lua


【解决方案1】:

无论你的脚本是从 c++ 还是从 Lua 命令行解释器启动的,模块的规则都是一样的。
您必须在 Lua 搜索器/加载器尝试找到它的路径中包含该模块。查看搜索的路径列表,将该 http dll(使用与您的项目相同的设置编译,以防 Lua 静态链接)放在搜索的路径之一中。 而且您必须将该模块与您的程序一起分发,不要指望它会安装在用户的电脑上。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-24
    • 1970-01-01
    • 2021-03-25
    • 1970-01-01
    • 1970-01-01
    • 2017-04-30
    • 2016-08-02
    • 2018-08-31
    相关资源
    最近更新 更多