gdxn96
gdxn96

Reputation: 371

Require Lua Module In Script Called from C++

I'm using VS2015 in a C++ application with Lua5.1. I'm running a very simple Lua script with no issue, raw lua works fine. but when I attempt to import a lua module "socket.http" my application doesn't like it because I imagine it can't find the module.

My question is how do I allow my lua script (being run from c++) to access lua modules like socket.http?

My 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("-- %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")

errors

 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'

Upvotes: 0

Views: 1375

Answers (1)

Vlad
Vlad

Reputation: 5847

Rules for modules is the same, no matter if you had script started from c++ or from a Lua command line interpreter.
You must have that module in the path where Lua searchers/loaders will try to find it. See the list of paths searched, put that http dll (compiled with same settings as your project, in case Lua is linked statically) in one of searched paths. And you have to distribute that module along with your program, don't expect it to be installed on user's pc.

Upvotes: 1

Related Questions