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

标签 c gcc linker lua cygwin

我在安装 cygwin 时使用的是带有 Lua Interpreter 包的 Cygwin 环境。 所以我能够编译和运行示例 lua 程序。 但是当我尝试执行一个包含 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
-----------------------------------------------------------------

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

最佳答案

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

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

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

关于在C文件中执行嵌入式lua编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5302398/

相关文章:

c - os_atomic_or2o 实际上做了什么?

用于调用函数的代码存储器(C ARM-GCC)

C - header 相互包含

c - 大小为 2^25 的数组

c - 如何在代码 .h 文件中组织声明和函数并包含以实现最佳代码重用

c++ - 带有自定义 argc 和 argv 的 getopt_long() 函数

c - 无内存重复的可内联常量 C 数组

c - 尽管满足条件,但循环不会停止

c++ - clang : warning: principal. o: 'linker' 输入未使用

gcc - 为什么 'Link Time Optimization' 会产生更大的二进制文件?