最近在学习使用Lua,也通过基本的语法知识完成了公司的一个关于配置文件参数合法性检查的小任务。虽然任务完成了,但对于一些函数的调用目的还是搞不明白,这两天再次重看了Manual Reference,稍微梳理出了一点眉目,记录在此。
首先看一段小小小程序
//test.lua
function printmsg()
print("hello world")
end
x = 10
//test.c
#include <stdio.h>
#include <unistd.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
int main(int argc, const char *argv[])
{
lua_State *L;
if(NULL == (L = luaL_newstate()))
{
perror("luaL_newstate failed");
return -1;
}
luaL_openlibs(L);
if(luaL_loadfile(L, "./test.lua"))
{
perror("loadfile failed");