Lua入门 - helloworld

一、安装lua

1.install readline

tar zxvf readline-5.2.tar.gz 

./configure --prefix=/usr/local/readline/

make && make install

ln -s /usr/local/readline/include/readline/ /usr/include/readline

cp /usr/local/readline/lib/*.a /usr/lib/

2.install lua

http://www.lua.org/download.html

tar zxvf lua-5.2.1.tar.gz 

make linux && make install

二、第一个lua脚本

打印 hello.lua

print"hello world!"

执行 lua hello.lua


三、在c中调用lua脚本

创建run_hellolua.c

#include <stdio.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>


lua_State * L;


int main(int argc, char ** argv)
{
        /* init lua*/
        L = luaL_newstate();


        /* load lua base lib*/
        luaL_openlibs(L);


        /* run hellolua script*/
        luaL_dofile(L, "hello.lua");


        /* clear lua*/
        lua_close(L);


        return 0;
}

编写makefile

run_hellolua:run_hellolua.c
        gcc -o run_hellolua run_hellolua.c -L/usr/local/lib/ -llua -lm -DLUA_USE_READLINE -ldl

make后执行./run_hellolua 输出 hello world!





你可能感兴趣的:(Lua入门 - helloworld)