c语言中嵌入lua

jamlee
  • 538

main.c 如下

#include <stdio.h>

#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
#include "luaconf.h"


int main(int argc, char* argv[])
{
    lua_State* L = luaL_newstate();
    luaL_openlibs(L);
    luaL_dofile(L, "D:\\__Development\\C\\delve\\Test_lua\\test.lua");
    lua_close(L);

    return 0;
}

test.lua如下

function show()
    local b = {}
    local index

    for index = 1,10,1 do
        print(index)
    end
end

show()

我是编译安的lua,但是执行的时候老报错,lua版本5.2

jamlee@ubuntu-jam:~/lab/lua$ ls /usr/local/bin/
lua  luac
jamlee@ubuntu-jam:~/lab/lua$ ls /usr/local/lib/
liblua.so  python2.7  python3.4
jamlee@ubuntu-jam:~/lab/lua$ 
jamlee@ubuntu-jam:~/lab/lua$ gcc -llua main.c
/tmp/cc1AF3u7.o: In function `main':
main.c:(.text+0xa): undefined reference to `luaL_newstate'
main.c:(.text+0x1a): undefined reference to `luaL_openlibs'
main.c:(.text+0x36): undefined reference to `luaL_loadfilex'
main.c:(.text+0x6e): undefined reference to `lua_pcallk'
main.c:(.text+0x7f): undefined reference to `lua_close'
collect2: error: ld returned 1 exit status
回复
阅读 4.8k
1 个回答
✓ 已被采纳

其中 -Lxx是lua.so 的目录

gcc main.c -llua -lm -ldl -Lxx -o test

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏