尝试编译lua文件时出现gcc错误

时间:2017-10-23 16:30:04

标签: gcc lua

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

1 个答案:

答案 0 :(得分:1)

由于您安装了liblua-5.1-dev,我假设您使用的是Debian或衍生产品。在那里,您必须与-llua5.1链接,如下所示:

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