【问题标题】:Compile embedded lua in C用 C 编译嵌入式 lua
【发布时间】:2011-11-06 15:52:33
【问题描述】:

大家好,我发现这段代码在 C 中嵌入了 Lua,但我不知道如何让 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 文件也在其中。

谢谢

谢谢

【问题讨论】:

  • 在命令行上可能类似于-llua。你为什么不向我们展示一些错误?
  • 感谢您的帮助,成功了。谢谢!

标签: c lua


【解决方案1】:

取决于你是要静态编译还是动态编译。

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

【讨论】:

  • 它也适用于共享库,所以 OP 应该尝试在命令行中添加 -llua、-llua5.1 和 -llua-5.1。
  • 如果在您的系统上可用,您可以使用gcc -o hello hello.o $(pkg-config --libs lua)
猜你喜欢
  • 1970-01-01
  • 2018-07-30
  • 2012-04-27
  • 1970-01-01
  • 2016-07-19
  • 2011-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多