为什么它总是说“未定义的参考”? [英] Why does it always say "undefined reference"?

查看:182
本文介绍了为什么它总是说“未定义的参考”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(我在Windows上)

(I'm on Windows.)

我正在测试我编译的DLL( libsox )与C程序,看起来这样:

I'm testing a DLL I have compiled (libsox) with a C program which looks this way:

#include <stdio.h>
#include "sox.h"

int main(void) {
    char const * versionText = sox_version();
    printf(versionText);
    return 0;
}

在DLL中定义的函数在 sox.h (其中包含 cdecl ):

The function that is defined in the DLL has the following prototype in sox.h (something of this contains cdecl):

LSX_RETURN_VALID_Z LSX_RETURN_PURE
char const *
LSX_API
sox_version(void);

以下是问题:当我尝试使用 gcc -lsox构建文件-o test.exe test.c 我收到以下错误:

Here's the problem: When I try to build the file with gcc -lsox -o test.exe test.c I get the following error:

C:\DOKUME~1\ADMINI~1\LOKALE~1\Temp\ccSS2h2z.o:test.c:(.text+0xf): undefined reference to `sox_version'
collect2: ld returned 1 exit status

一个字到 -lsox :我有MinGW的 lib 文件夹中的库文件libsox.dll.a。如果我写了 -lsoxnonsense ,那么它说没有图书馆。这意味着在所示的情况下它会找到库。那么为什么不想创建一个链接。

A word to -lsox: I have the library file "libsox.dll.a" in MinGW's lib folder. If I write -lsoxnonsense, then it says there is no library. That means in the shown case it finds the library. So why doesn't it want to create a link.

推荐答案

gcc -lsox -o test.exe test.c

您必须先放置源文件:

gcc test.c -lsox -o test.exe

这是因为链接器按顺序浏览输入文件,找到未定义的引用并满足之前看到的引用。所以在你的命令行中,它读取 libsox.a (或类似的东西),找到未定义的引用(将不会有)。然后,它进入你的 test.c ,在那里找到未定义的引用,但没有更多的库来满足他们。

It is because the linker goes through the input files in order, finding undefined references and satisfying references that it saw before. So in your command line, it reads libsox.a (or something like that), finds undefined references (there would be none). Then, it goes to your test.c, finds undefined references in there, but there are no further libraries to satisfy them.

有关详细信息,请参阅此答案。

这篇关于为什么它总是说“未定义的参考”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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