lua require path setting example

Baidu article can't understand, write one yourself.

1. The first thing to emphasize is that the path of lua require uses a slash "/" instead of the backslash "\" copied from the Windows file properties.

2. By printing the two global properties encapsulated by the lua system through print(pagckage.path) and print(package.cpath), you can see the path replaced by default when the current lua parser requires

3. When changing the path, add ";..\\?.lua" to the lua script and ";..\\?.dll" to the dll file 

Next, I want to require the lua script whose path is "D:\Lua5.1\lua\hgj-test\huang.lua" into the hgj.lua script.

huang.lua script:

huang={}
function huang.SumDouble(max)
sum=0
for i=0,max,2 do
sum=sum+i
end
return sum

end


hgj.lua script:

package.path = "D:/Lua5.1/lua/hgj-test/huang.lua" ..";..\\?.lua"

require"huang.lua"

print(huang.SumDouble(100))
print(package.path)
print(package.cpath)


The result printed by pressing F5 in hgj.lua is:


2550
D: /Lua5.1/lua/hgj-test/huang.lua; .. \ ?. lua
. \ ?. dll;. \? 51.dll; D: \ Lua5.1 \ ?. dll; D: \ Lua5.1 \? 51.dll; D: \ Lua5.1 \ clibs \ ?. dll; D: \ Lua5.1 \ clibs \? 51.dll; D: \ Lua5.1 \ loadall.dll; D: \ Lua5.1 \ clibs \ loadall.dll

Because the encoding is different, the garbled code is generated when loading the dll. If you know the attributes and functions encapsulated in the dll, you can use print(package.loadlib("--dll name--", "--attribute name/function name after loading) --")) to read the properties and functions of the dll.

I casually loaded a dll from the DingTalk software used by the company to punch cards:

package.path = "D:/Program Files (x86)/DingDing/main/current/alilog.dll"..";..\\?.dll"
require"alilog.dll"

--package.loadlib("alilog.dll","不知道函数名")

A garbled code is loaded, I can't read it, and I don't know what functions are in it. I can't use it, it can only be used as a demonstration.


Of course, if you don't need to call the properties or functions of other scripts, but directly execute a whole other script, or dofile ("file path") is comfortable, directly

dofile ("D: /Lua5.1/lua/hgj-test/huang.lua"

That's it, but there is one point, when the code is designed, the function in the called script must be executed by itself. If you only encapsulate the attribute function and don't execute it, then you can't use the dofile route.


Original text: https://blog.csdn.net/piger91/article/details/79940663





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324606012&siteId=291194637