luajit——luajit源码安装——编译运行lua
1、源码安装luajit
下载luajit源码 http://luajit.org/download.html
git clone https://luajit.org/git/luajit.git
a. 以 LuaJIT-2.1.0-beta3.tar.gz 为例, 下载解压后进入 LuaJIT-2.1.0-beta3目录
make
make install
这样会把luajit安装到系统目录, /usr/local下
b. 若需指定安装目录,需在执行 make install 之前,修改Makefile文件
Makefile中的安装路径 #export PREFIX= /usr/local DIR=$(shell pwd) export PREFIX=$(DIR)/build export MULTILIB= lib
便可安装到与Makefile文件同目录的build文件夹下。
2、编译运行lua
编译lua luajit -b test.lua test.luac
执行luac luajit test.luac
报错处理:
luajit -b 编译报错: ./luajit: unknown luaJIT command or jit.* modules not installed
解决办法:
无论编译好的 luajit 命令在什么目录,
当前执行 "luajit -b 源文件 编译文件" 的目录下,必须存在jit文件夹,jit文件夹在luajit源码的src文件夹。
错误重现
bogon:bin chong$ ls -ls total 1104 8 lrwxr-xr-x 1 chong staff 18 13 Gen 11:32 luajit -> luajit-2.1.0-beta3 1096 -rwxr-xr-x 1 chong staff 559796 13 Gen 11:30 luajit-2.1.0-beta3 bogon:bin chong$ bogon:bin chong$ ./luajit -b ../../../test.lua ../../../test.luac ./luajit: unknown luaJIT command or jit.* modules not installed bogon:bin chong$
进入到包含jit文件夹的目录,可以正确编译
bogon:build chong$ ls -ls total 0 0 drwxr-xr-x 4 chong staff 136 13 Gen 12:18 bin 0 drwxr-xr-x 3 chong staff 102 13 Gen 11:30 include 0 drwxr-xr-x 19 chong staff 646 13 Gen 11:57 jit 0 drwxr-xr-x 8 chong staff 272 13 Gen 11:30 lib 0 drwxr-xr-x 5 chong staff 170 13 Gen 11:30 share bogon:build chong$ bogon:build chong$ bin/luajit -b ../../test.lua ../../test.luac bogon:build chong$ bogon:build chong$
3、直接执行lua的示例
test.lua代码
print("hi ... ")
直接执行
bogon:c chong$ LuaJIT/build/bin/luajit test.lua
hi ...
bogon:c chong$
4、lua 读写文件
function Reader:saveJson() for tableName, v in pairs(configs) do self:getDataTable(tableName) local content = json.encode(self._Tables[tableName]) local outpath = "/Users/json_out/" --local content = "i am temp..." local file = io.open(outpath..tableName..".json", "w"); file:write(content); file:close(); end print("ok") end