【问题标题】:gcc error on trying to compile lua file尝试编译 lua 文件时出现 gcc 错误
【发布时间】:2018-04-04 06:54:59
【问题描述】:

我正在尝试使用以下方法从 lua 文件创建可执行文件:

我使用 bintocee 实用程序(来自:http://lua-users.org/wiki/BinToCee)将 myfile.lua 转换为 code.c 。然后我使用以下 main.c(来自:Creating standalone Lua executables

#include <stdlib.h>
#include <stdio.h>

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

int main(int argc, char *argv[]) {
  int i;
  lua_State *L = luaL_newstate();
  luaL_openlibs(L);
  lua_newtable(L);
  for (i = 0; i < argc; i++) {
    lua_pushnumber(L, i);
    lua_pushstring(L, argv[i]);
    lua_rawset(L, -3);
  }
  lua_setglobal(L, "arg");
#include "code.c" 
  lua_close(L);
  return 0;
}

然后我发出命令:

gcc main.c -o myfile.exe 

但是,我收到以下错误:

/tmp/ccyIOC0O.o: In function `main':
main.c:(.text+0x21): undefined reference to `luaL_newstate'
main.c:(.text+0x2f): undefined reference to `luaL_openlibs'
main.c:(.text+0x41): undefined reference to `lua_createtable'
main.c:(.text+0x62): undefined reference to `lua_pushnumber'
main.c:(.text+0x82): undefined reference to `lua_pushstring'
main.c:(.text+0x92): undefined reference to `lua_rawset'
main.c:(.text+0xb7): undefined reference to `lua_setfield'
main.c:(.text+0xd5): undefined reference to `luaL_loadbuffer'
main.c:(.text+0xea): undefined reference to `lua_pcall'
main.c:(.text+0xf8): undefined reference to `lua_close'
collect2: error: ld returned 1 exit status

我正在开发 Linux Debian Stable(已更新)。问题出在哪里,如何解决?感谢您的帮助。

【问题讨论】:

  • lua 有一点想法,但它显然需要链接一些库。
  • 我尝试安装 liblua5.1 和 liblua5.1-dev 但同样的错误仍然存​​在。
  • 你需要链接。 gcc main.c -llua -o myfile.exe 之类的东西(不知道实际名称)

标签: gcc lua


【解决方案1】:

由于您安装了liblua-5.1-dev,我假设您使用的是 Debian 或衍生产品。在那里,你必须链接到-llua5.1,像这样:

gcc -O2 -Wall -I/usr/include/lua5.1 main.c -llua5.1

【讨论】:

  • 是的,它有效。如果您能简要解释此处使用的所有选项,将对所有人都有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多