marcus1337
marcus1337

Reputation: 41

local socket = require("socket"), module 'socket' not found

Im using Lua with C++ in a project in Visual Studio 2015. I have used Luarocks to create socket/core.dll and mime/core.dll. I have added the core.dll to the debug folder where my C++ program executes. The error I get in lua is generated when "require("socket")" executes. The following error is what I get:

...s\Visual Studio 2015\Projects\RaceGame3\Debug\Client.lua:17: module 
'socket' not found:
    no field package.preload['socket']
    no file 'C:\Users\Username\Documents\Visual Studio         
2015\Projects\RaceGame3\Debug\lua\socket.lua'
    no file 'C:\Users\Username\Documents\Visual Studio 2015\Projects\RaceGame3\Debug\lua\socket\init.lua'
    no file 'C:\Users\Username\Documents\Visual Studio 2015\Projects\RaceGame3\Debug\socket.lua'
    no file 'C:\Users\Username\Documents\Visual Studio 2015\Projects\RaceGame3\Debug\socket\init.lua'
    no file 'C:\Users\Username\Documents\Visual Studio 2015\Projects\RaceGame3\Debug\..\share\lua\5.3\socket.lua'
    no file 'C:\Users\Username\Documents\Visual Studio 2015\Projects\RaceGame3\Debug\..\share\lua\5.3\socket\init.lua'
    no file '.\socket.lua'
    no file '.\socket\init.lua'
    no file 'C:\Users\Username\Documents\Visual Studio 2015\Projects\RaceGame3\Debug\socket.dll'
    no file 'C:\Users\Username\Documents\Visual Studio 2015\Projects\RaceGame3\Debug\..\lib\lua\5.3\socket.dll'
    no file 'C:\Users\Username\Documents\Visual Studio 2015\Projects\RaceGame3\Debug\loadall.dll'
    no file '.\socket.dll'

So to sum up: How do I correctly link the core.dll or other luasocket files to my current Lua instance while running the C++ project?

Upvotes: 1

Views: 6618

Answers (2)

ChangUk
ChangUk

Reputation: 135

You were in a very similar situation to me. In my case, I required socket.http in sample.lua,

require("socket.http")
content, status, header = socket.http.request("http://website.com/aaa.php")

and have met the following error message:

...\MyProject\Release\sample.lua:1: module 'socket.http' not found:
    no field package.preload['socket.http']
    no file '.\socket\http.lua'
    (...)

I've addressed this issue by putting some necessary lua scripts and dll files in the path where a sample executable is located.

Release
├── socket
│   ├── ftp.lua
│   ├── http.lua
│   ├── smtp.lua
│   ├── tp.lua
│   └── url.lua
├── mime
│   └── core.dll
├── ltn12.lua
├── mime.lua
├── socket.dll    <--- renamed from $(LUA_PATH)\clibs\socket\core.dll
├── socket.lua
├── lua5.1.dll
├── sample.exe
└── sample.lua

The cpp code is as follows:

#pragma comment(lib, "lua5.1.lib")
#include <lua.hpp>
void main() {
    lua_State *L = luaL_newstate();
    luaL_openlibs(L);
    luaL_dofile(L, "sample.lua");
    lua_close(L);
}

Upvotes: 1

Lucas Siqueira
Lucas Siqueira

Reputation: 845

I've solved a similar issue by changing the require to:

require("socket.core")

That only works, of course, if you have the core.dll inside a folder named socket that can be found locally or in your PATH / package.cpath, etc..

You could also rename core.dll to socket.dll (and place it in a searchable folder).

The problem, as far as I know, is: the required name and the actual dll name simply doesn't match.

edit: To play safe, I've put the lua modules and the dll together, locally, like this:

socket
├── core.dll
├── ltn12.lua
├── mime.lua
├── mime-1.0.3.dll
├── socket
│   ├── ftp.lua
│   ├── headers.lua
│   ├── http.lua
│   ├── smtp.lua
│   ├── tp.lua
│   └── url.lua
└── socket.lua

Upvotes: 1

Related Questions