安装Lua套接字库 [英] Installing Lua socket library

查看:192
本文介绍了安装Lua套接字库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我太过劳累或失明了.我想学习与Lua的联网,因此必须安装socket lib,这样我可以轻松地要求它,但是我不知道应该需要"哪些文件.该示例说:

Either I'm overtired or blind. I want to learn networking with Lua and therefore I have to install the socket lib, so I can require it easily, but I don't know, which files I should "require". The example says:

local socket = require("socket")

但是正如我所说,我不知道应该包含哪些文件,如果使用socket.lua则不起作用,我得到:No files found.

but as I said, I don't know which files I should include, if I use socket.lua it doesn't work and I get: No files found.

我从这里得到文件库: Lua套接字下载

I got the lib from here: Lua socket download

或者,还有另一种安装套接字库的方法吗?

Or, is there another way to install the socket lib?

推荐答案

在使用require加载模块时,Lua使用包路径来确定在何处查找模块.请查看Lua手册的模块部分.具体来说,关于package.pathpackage.cpath的部分.

When you load a module with require Lua uses the package paths to determine where to look for the module. Have a look at the Modules section of the Lua manual. Specifically, the section on package.path and package.cpath.

package.path:require用于搜索Lua加载程序(.lua模块)的路径
package.cpath:require用于搜索C加载程序(.so/.dll模块)的路径

package.path: The path used by require to search for a Lua loader (.lua modules)
package.cpath: The path used by require to search for a C loader (.so/.dll modules)

您可以检查当前路径是什么

You can check what the current paths are:

print(package.path..'\n'..package.cpath)

如果将LuaSocket安装到当前程序包路径中的某个位置,Lua应该能够找到并加载它.

If you install LuaSocket into a location within your current package paths Lua should be able to locate and load it.

或者,您可以在调用require之前修改包路径.例如,如果您为项目创建一个文件夹,然后将LuaSocket库提取到项目文件夹中名为libs的子文件夹中:

Alternatively, you can modify the package paths before calling require. For example, if you create a folder for your project and extract the LuaSocket library to a sub-folder called libs within your project folder:

Project
|
> libs
     |
     > lua
         |
         > socket         
     > socket
     > mime

您可以先设置相对于项目的程序包路径,然后再require套接字库(在Linux上将/?.dll替换为/?.so):

You can set the package paths relative to your project before you require the socket library (substitute /?.dll for /?.so on Linux):

package.path = package.path..';./libs/lua/?.lua'
package.cpath = package.cpath..';./libs/socket/?.dll;./libs/mime/?.dll'
local socket = require 'socket'

这篇关于安装Lua套接字库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆