无法链接最小的 Lua 程序 [英] Cannot link a minimal Lua program

查看:14
本文介绍了无法链接最小的 Lua 程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从《Programming In Lua》一书中复制了以下简单的 Lua 程序

I have the following trivial Lua program which I copied from the book Programming In Lua

#include <stdio.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
int main (void) 
{
    char buff[256];
    int error;
    lua_State *L = luaL_newstate(); /* opens Lua */
    luaL_openlibs(L); /* opens the standard libraries */
    while (fgets(buff, sizeof(buff), stdin) != NULL) 
    {
        error = luaL_loadbuffer(L, buff, strlen(buff), "line") ||
        lua_pcall(L, 0, 0, 0);

        if (error) 
        {
            fprintf(stderr, "%s", lua_tostring(L, -1));
            lua_pop(L, 1); /* pop error message from the stack */
        }
    }
    lua_close(L);
    return 0;
}

我的环境是cywin

我的 make 文件如下所示:

my make file looks like this:

CC=gcc
INCLUDE='-I/home/xyz/c_drive/Program Files/Lua/5.1/include'
LINKFLAGS='-L/home/xyz/c_drive/Program Files/Lua/5.1/lib' -llua51 

li.o:li.c
    $(CC) $(INCLUDE)   -c li.c

main:li.o
    $(CC)  -o main  $(LINKFLAGS) li.o 

clean:
    rm *.o
    rm main

我的/home/xyz/c_drive/Program Files/Lua/5.1/lib目录下有lua5.1.dll lua5.1.lib lua51.dll和lua51.lib

My /home/xyz/c_drive/Program Files/Lua/5.1/lib directory contains lua5.1.dll lua5.1.lib lua51.dll and lua51.lib

尝试构建我的主要目标时出现以下错误:

Trying to build my main target I am getting the following errors:

li.o:li.c:(.text+0x35): undefined reference to `_luaL_newstate'
li.o:li.c:(.text+0x49): undefined reference to `_luaL_openlibs'
li.o:li.c:(.text+0xaf): undefined reference to `_luaL_loadbuffer'
li.o:li.c:(.text+0xd9): undefined reference to `_lua_pcall'
li.o:li.c:(.text+0x120): undefined reference to `_lua_tolstring'
li.o:li.c:(.text+0x154): undefined reference to `_lua_settop'
li.o:li.c:(.text+0x167): undefined reference to `_lua_close'

关于我在这里可能做错了什么有什么想法吗?

Any ideas about what I might be doing wrong here?

推荐答案

问题是您在链接命令行上需要它们的目标文件之前命名了库.链接器在命令行上从左到右加载模块.在您命名为 -llua51 的那一行,没有已知该库可以满足的未定义符号.然后你命名 li.o,它确实有未知符号.

The problem is that you have named the libraries on the link command line before the object files that require them. The linker loads modules from left to right on the command line. At the point on the line where you name -llua51, no undefined symbols that could be satisfied by that library are known. Then you name li.o, which does have unknown symbols.

某些类 Unix 环境不会将此视为错误,因为当对 .so 文件的引用得到满足时,部分链接过程会延迟到程序加载.但是 Cygwin、MinGW 和 Windows 通常必须将此视为错误,因为 DLL 的工作方式与 .so 文件完全不同.

Some Unix-like environments don't treat this as an error because part of the link process is deferred to the program load when reference to .so files are satisfied. But Cygwin, MinGW, and Windows in general must treat this as an error because DLLs work quite differently from .so files.

解决方案是将 -llua51 after 放在链接行上的所有 .o 文件之后.

The solution is to put -llua51 after all the .o files on your link line.

顺便说一句,您似乎正在链接 Windows 发行版的 Lua,但在 Cygwin 下使用 GCC 进行构建.您将需要使用 Dependency Walker 来确保您的程序不依赖于 Cygwin 运行时,并且它确实依赖于与 Lua for Windows 中的 lua51.dll 相同的 C 运行时.IIRC,这将是以前版本的 Visual Studio 的运行时.可以针对它进行 GCC 链接,但您需要使用 MinGW 端口(您可以从 Cygwin 使用),并链接到几个特定的​​库以获得该版本.我远离我常用的电脑,或者我会引用一个确切的链接行.(我相信您需要 -lmoldname -lmsvcr80 或类似的东西,作为链接行上的最后一项.)

Incidentally, it appears you are linking against the Lua for Windows distribution, but building with GCC under Cygwin. You will want to use Dependency Walker to make sure that your program does not depend on the Cygwin runtime, and that it does depend on the same C runtime as the lua51.dll from Lua for Windows. IIRC, that will be the runtime for the previous version of Visual Studio. It is possible to make GCC link against that, but you will need to be using the MinGW port (which you can use from Cygwin), and link against a couple of specific libraries to get that version. I'm away from my usual PC, or I'd quote an exact link line. (I believe you need -lmoldname -lmsvcr80 or something like that, as the last items on the link line.)

如果使用多个 C 运行时库,将导致神秘且难以诊断的问题.简单的答案是使用与您首选的 Lua DLL 相同的一个.另一种选择是 Lua Binaries 项目 已经为 Windows 上的各种 C 工具链预编译了 Lua DLL.如果您需要一个了解 Cygwin 环境的 Lua 应用程序,您将需要一个由 GCC 为 Cygwin 构建的应用程序,而不是用于 Windows 风格的 Lua.Lua 二进制文件将成为您的朋友,或者您可以从源代码构建自己的 Lua.

It will cause mysterious and very hard to diagnose problems if more than one C runtime library is in use. The easy answer is to use the same one as your preferred Lua DLL. Another alternative is that the Lua Binaries project has pre-compiled Lua DLLs for a wide array of C toolchains on Windows. If you need a Lua application that understands the Cygwin environment, you will want one that is built by GCC for Cygwin and not the Lua for Windows flavor. Lua Binaries will be your friend, or you can build Lua your self from source.

这篇关于无法链接最小的 Lua 程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆