在C中编译嵌入式lua。

大家好,我找到了一个能将 Lua 嵌入 C 代码中的程序,但我不知道如何让 GCC 编译它。我已经安装了 Lua,但是我该如何链接 Lua 库呢?

以下是我找到的代码:

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

            /* lua interpreter */
            lua_State* l;

            int main () {
            int dofile;

            /* initialize lua */
            l = lua_open();

            /* load lua libraries */
            luaL_openlibs(l);

            /* run the hello.lua script */
            dofile = luaL_dofile(l, "hello.lua");

            if (dofile == 0) {
            /* call foo */
            lua_getglobal(l,"foo");
            lua_call(l,0,0);
            }
            else {
            printf("Error, unable to run hello.lua\n");
            }

            /* cleanup Lua */
            lua_close(l);

            return 0;
            }

我该如何编译它?

我试图使用以下命令进行编译:

gcc -o embed_hello -L/users/etrosclair/Downloads/lua-5.1.4 -I/users/etrosclair/Downloads/lua-5.1.4 luaTest.c

这里是错误:

Undefined symbols for architecture x86_64:
  "_luaL_newstate", referenced from:
      _main in ccF0995Q.o
  "_luaL_openlibs", referenced from:
      _main in ccF0995Q.o
  "_luaL_loadfile", referenced from:
      _main in ccF0995Q.o
  "_lua_pcall", referenced from:
      _main in ccF0995Q.o
  "_lua_getfield", referenced from:
      _main in ccF0995Q.o
  "_lua_call", referenced from:
      _main in ccF0995Q.o
  "_lua_close", referenced from:
      _main in ccF0995Q.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

所有的 Lua 库和头文件都在 lua-5.1.4 文件夹中,.o 文件也在其中。

谢谢。

原文链接 https://stackoverflow.com/questions/8024498

点赞
stackoverflow用户282536
stackoverflow用户282536

。 对于静态编译,需要添加-llua(或lua5.1或lua51;取决于您的设置)。

2011-11-06 07:52:33