尝试编译 Lua 文件时出现的 GCC 错误

我正在尝试使用以下方法将 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 进行工作(已更新)。 问题在哪里,如何解决? 谢谢你的帮助。

点赞
用户8316315
用户8316315

由于您已安装 liblua-5.1-dev,我假定您使用的是 Debian 或其衍生版。因此,您需要使用 -llua5.1 进行链接,就像这样:

gcc -O2 -Wall -I/usr/include/lua5.1 main.c -llua5.1
2017-10-23 16:42:12