下面是c语言的一个执行lua脚本的东东,可以把lua文件拖拽到exe上运行,或者默认绑定当前目录下的 1.lua 文件
头文件 和 lua.lib 的位置视自己的情况而定,conio.h头文件只是为了让每执行一次后用 getch()读取任意字符且不显示,如编译不过可去掉conio.h 把getch()换成getchar()
#include <stdio.h>
#include <conio.h>
extern "C"{
#include "G:\\库\\lua\\lua-5.2.3\\src\\lua.h"
#include "G:\\库\\lua\\lua-5.2.3\\src\\lualib.h"
#include "G:\\库\\lua\\lua-5.2.3\\src\\lauxlib.h"
}
#pragma comment(lib, "c:\\lua\\lua.lib")
int main(int argc, char *argv[])
{
char *p;
if (argc >= 2)
{
p = argv[1];
}
else
{
p = "1.lua";
}
printf("绑定:%s\n", p);
while (1)
{
lua_State * L = luaL_newstate();
luaL_openlibs(L);
luaL_dofile(L, p);
lua_close(L);
printf("执行完成!\n");
getch();
}
return 0;
}
1.lua文件中的一些基本的演示
b = 10 --值为非nil时变量存在,单行注释
print (b)
b = nil
print (b)
print("类型:")
print(type('hello'))
print(type(10*3))
print(type(1.0*2.3))
print(type(print))
print(type(true))
print(type(nil))
print(type(type(x)))
print('字符串:')
page = [[
#include <stdio.h>
int main()
{
return 0;
}
]] --存储字符串
io.write(page)
print('10' *5) --string转成数字
print(1 + 8) --数字转成string
print('abc' .. 'de') --链接字符串
print('输入一行,转成数字')
line = io.read()
n = tonumber(line)
if n == nil then
print(line .. '无法转成数字')
error(line .. '无法转成数字') --error会直接结束运行
else
print('转换后:' .. n)
end
print(tostring(10) == '10')
print(10 .. '' == '10')