【问题标题】:compilation error on Executing embedded lua in C file在 C 文件中执行嵌入式 lua 时出现编译错误
【发布时间】:2011-03-14 17:55:34
【问题描述】:

我在安装 cygwin 时使用带有 Lua Interpreter 包的 Cygwin 环境。 所以我能够编译和运行示例 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
-----------------------------------------------------------------

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

【问题讨论】:

    标签: c gcc linker lua cygwin


    【解决方案1】:

    -llua5.1 放在../samples/ctest.c 之后。对象应该以相反的依赖顺序链接。

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

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

    【讨论】:

    • $ cc -o ctest ctest.c /usr/lib/liblua.a
    猜你喜欢
    • 2018-04-04
    • 1970-01-01
    • 2020-10-17
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 2018-09-22
    • 2010-10-17
    • 1970-01-01
    相关资源
    最近更新 更多