2017-03-02 81 views
0

我在使用Lua5.1的C++應用程序中使用VS2015。我正在運行一個非常簡單的Lua腳本,沒有問題,原始lua工作正常。但是當我嘗試導入一個lua模塊「socket.http」時,我的應用程序不喜歡它,因爲我想它無法找到該模塊。要求在C++腳本中調用Lua模塊

我的問題是如何讓我的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("-- %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' 
+1

'socket.http'不是原生的Lua模塊,也許你安裝了它? – bew

回答

0

規則模塊是一樣的,不管你有腳本從C++或啓動來自Lua命令行解釋器。
您必須在Lua搜索器/加載器試圖找到它的路徑中擁有該模塊。查看搜索到的路徑列表,將http dll(在您的項目中使用相同的設置進行編譯,如果Lua靜態鏈接)搜索到的路徑之一。 而且你必須隨程序一起分發這個模塊,不要期望它被安裝在用戶的電腦上。