在C文件中执行嵌入式lua时出现编译错误

时间:2011-03-14 17:55:35

标签: c gcc linker lua cygwin

我正在使用Cygwin环境,在安装cygwin时包含Lua Interpreter包。 所以我能够编译和运行示例lua progs。 但是当我尝试执行具有lua调用的示例c文件时,我总是会收到以下错误。

$ cc -o ../samples/ctest -Wall  ../samples/ctest.c
/tmp/ccOYgLj4.o:ctest.c:(.text+0x2b): undefined reference to `_luaL_newstate'
/tmp/ccOYgLj4.o:ctest.c:(.text+0x3d): undefined reference to `_luaL_openlibs'
/tmp/ccOYgLj4.o:ctest.c:(.text+0x59): undefined reference to `_luaL_loadfile'
/tmp/ccOYgLj4.o:ctest.c:(.text+0x82): undefined reference to `_lua_pcall'
/tmp/ccOYgLj4.o:ctest.c:(.text+0xb8): undefined reference to `_lua_getfield'
/tmp/ccOYgLj4.o:ctest.c:(.text+0xd5): undefined reference to `_lua_call'
/tmp/ccOYgLj4.o:ctest.c:(.text+0xf0): undefined reference to `_lua_close'
collect2: ld returned 1 exit status

我的示例ctest.c文件内容:

#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;
}

hello.lua file contents:
print("from c hurray")

在网上搜索他们说一些链接器错误并且必须包含-llua51。所以我尝试了以下内容。

$ cc -o ../samples/ctest -Wall -llua5.1 ../samples/ctest.c
/tmp/cc3v5Nim.o:ctest.c:(.text+0x2b): undefined reference to `_luaL_newstate'
/tmp/cc3v5Nim.o:ctest.c:(.text+0x3d): undefined reference to `_luaL_openlibs'
/tmp/cc3v5Nim.o:ctest.c:(.text+0x59): undefined reference to `_luaL_loadfile'
/tmp/cc3v5Nim.o:ctest.c:(.text+0x82): undefined reference to `_lua_pcall'
/tmp/cc3v5Nim.o:ctest.c:(.text+0xb8): undefined reference to `_lua_getfield'
/tmp/cc3v5Nim.o:ctest.c:(.text+0xd5): undefined reference to `_lua_call'
/tmp/cc3v5Nim.o:ctest.c:(.text+0xf0): undefined reference to `_lua_close'
collect2: ld returned 1 exit status

Vedhashree@Vedhashree-PC /cygdrive/c/cygwin/bin
$ ls /usr/lib/liblua*.a
/usr/lib/liblua.a      /usr/lib/liblua5.1.a
/usr/lib/liblua.dll.a  /usr/lib/liblua5.1.dll.a

你能帮我解决这个问题并使我的第一个嵌入式lua c程序工作吗?

更新

$ cc -o ctesing -Wall ctesting.c -llua5.1
/usr/lib/gcc/i686-pc-cygwin/4.3.4/../../../../i686-pc-cygwin/bin/ld: cannot find
 -llua5.1
collect2: ld returned 1 exit status

-----------------------------------------------------------------
cc -o ../samples/ctest -Wall ../samples/ctest.c -llua 
/usr/lib/gcc/i686-pc-cygwin/4.3.4/../../../../i686-pc-cygwin/bin/ld: cannot find
 -llua51
collect2: ld returned 1 exit status
-----------------------------------------------------------------

cc -o ../samples/ctest -Wall ../samples/ctest.c -llua51 
/usr/lib/gcc/i686-pc-cygwin/4.3.4/../../../../i686-pc-cygwin/bin/ld: cannot find
 -llua
collect2: ld returned 1 exit status
-----------------------------------------------------------------

我仍然只得到这些错误:(

1 个答案:

答案 0 :(得分:5)

-llua5.1之后放置../samples/ctest.c。对象应按相反的顺序链接。

cc -o ../samples/ctest -Wall ../samples/ctest.c -llua5.1

更新:您的更新描述了其他问题。在这种情况下,链接器无法在其搜索路径中找到liblua5.1.a文件。确保您的系统上有这样的库,并尝试使用-L option添加其路径。