Lua判断OS并添加cpath

来源:互联网 发布:下载炫浪网络社区app 编辑:程序博客网 时间:2024/05/24 00:53
Lua判断OS并添加cpath

(金庆的专栏)

Lua初始化时需要根据OS来设置package.cpath,
如果是Windows系统则添加 ?.dll, 否则添加 ?.so.
不然加载错误后缀名的动态库会报错。

local function add_package_cpath(subDir)
    if is_windows then
        package.cpath = package.cpath..";"..G_LUA_ROOTPATH.."/"..subDir.."/?.dll"
    else
        package.cpath = package.cpath..";"..G_LUA_ROOTPATH.."/"..subDir.."/?.so"
    end
end

Lua没有提供OS判断功能,所以利用 package.config 中的目录分隔符来判断。

-- return true if os is windows
local function get_is_windows()
    return "\\" == package.config:sub(1,1)
end

local is_windows = get_is_windows()

0 0
原创粉丝点击