lua是非常简单的脚本语言,我们以一个简单的例子开始(假设文件名字为my.lua)结果为:
另外,也可以在C语言中调用lua脚本。具体例子(test_lua.c)如下所示:
- #include <lua.h>
- #include <lauxlib.h>
- #include <stdlib.h> /* For function exit() */
- #include <stdio.h> /* For input/output */
- void bail(lua_State *L, char *msg){
- fprintf(stderr, "\nFATAL ERROR:\n %s: %s\n\n",
- msg, lua_tostring(L, -1));
- exit(1);
- }
- int main(int argc, const char *argv[])
- {
- if(argc != 2)
- {
- return 1;
- }
- lua_State *L = luaL_newstate(); /* Create new lua state variable */
- /* Load Lua libraries, otherwise, the lua function in *.lua will be nil */
- luaL_openlibs(L);
- if( luaL_loadfile(L,argv[1]) ) /* Only load the lua script file */
- bail(L, "luaL_loadfile() failed");
- if( lua_pcall(L,0,0,0) ) /* Run the loaded lua file */
- bail(L, "lua_pcall() failed");
- lua_close(L); /* Close the lua state variable */
- return 0;
- }
编译:
$ gcc -g ./test_lua.c -llua
执行:
$ ./a.out my.lua
Hello world
阅读(4731) | 评论(0) | 转发(0) |