在脚本中需要LUA模块,称为C

Require Lua Module In Script Called from C++

本文关键字:模块 称为 LUA 脚本      更新时间:2023-10-16

我正在使用LUA5.1的C 应用程序中使用VS2015。我正在运行一个非常简单的LUA脚本,没有问题,RAW LUA工作正常。但是,当我尝试导入LUA模块" socket.http"时,我的应用程序不喜欢它,因为我认为它找不到该模块。

我的问题是如何允许我的lua脚本(从C 运行)访问lua模块,例如socket.http?

我的project.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("-- %sn", 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 '.sockethttp.lua'
    no file 'C:UsersgeorgDesktopgitARGOGameATracknophiliaDebugluasockethttp.lua'
    no file 'C:UsersgeorgDesktopgitARGOGameATracknophiliaDebugluasockethttpinit.lua'
    no file 'C:UsersgeorgDesktopgitARGOGameATracknophiliaDebugsockethttp.lua'
    no file 'C:UsersgeorgDesktopgitARGOGameATracknophiliaDebugsockethttpinit.lua'
    no file 'C:Program Files (x86)Lua5.1luasockethttp.luac'
    no file '.sockethttp.dll'
    no file '.sockethttp51.dll'
    no file 'C:UsersgeorgDesktopgitARGOGameATracknophiliaDebugsockethttp.dll'
    no file 'C:UsersgeorgDesktopgitARGOGameATracknophiliaDebugsockethttp51.dll'
    no file 'C:UsersgeorgDesktopgitARGOGameATracknophiliaDebugclibssockethttp.dll'
    no file 'C:UsersgeorgDesktopgitARGOGameATracknophiliaDebugclibssockethttp51.dll'
    no file 'C:UsersgeorgDesktopgitARGOGameATracknophiliaDebugloadall.dll'
    no file 'C:UsersgeorgDesktopgitARGOGameATracknophiliaDebugclibsloadall.dll'
    no file '.socket.dll'
    no file '.socket51.dll'
    no file 'C:UsersgeorgDesktopgitARGOGameATracknophiliaDebugsocket.dll'
    no file 'C:UsersgeorgDesktopgitARGOGameATracknophiliaDebugsocket51.dll'
    no file 'C:UsersgeorgDesktopgitARGOGameATracknophiliaDebugclibssocket.dll'
    no file 'C:UsersgeorgDesktopgitARGOGameATracknophiliaDebugclibssocket51.dll'
    no file 'C:UsersgeorgDesktopgitARGOGameATracknophiliaDebugloadall.dll'
    no file 'C:UsersgeorgDesktopgitARGOGameATracknophiliaDebugclibsloadall.dll'

模块的规则是相同的,无论您是从C 启动的脚本还是从LUA命令行解释器。
您必须在LUA搜索器/加载程序尝试找到它的路径中拥有该模块。请参阅所搜索的路径列表,放置在一个搜索路径中,HTTP DLL(以Lua静态链接为lua,以与您的项目相同的设置进行了编译)。而且您必须与程序一起分发该模块,不要指望它安装在用户的PC上。