【问题标题】:Undefined reference on very simple program非常简单的程序上的未定义引用
【发布时间】:2012-01-14 22:47:19
【问题描述】:

一旦我安装了 Ubuntu 11.10,就会出现奇怪的错误。我想在我的 C 程序中使用 GD,所以我安装了包“libgd2-xpm-dev”。一切都安装好了——文件 gd.h 和 libgd.a 在“/usr/include”和“/usr/lib”中。所以,我尝试用 GD 编译简单的程序。

#include <stdio.h>
#include <gd.h>

int main()
{
        gdImagePtr im, im_clear;
        int black, white;
        FILE *out1;

        im = gdImageCreate(100, 100);
        im_clear = gdImageCreate(100, 100);

        white = gdImageColorAllocate(im, 255, 255, 255);
        black = gdImageColorAllocate(im, 0, 0, 0);
        return 0;
}

$ gcc -lgd gd.c
/tmp/cc6LReuX.o: In function `main':
gd2.c:(.text+0x19): undefined reference to `gdImageCreate'
gd2.c:(.text+0x31): undefined reference to `gdImageCreate'
gd2.c:(.text+0x59): undefined reference to `gdImageColorAllocate'
gd2.c:(.text+0x81): undefined reference to `gdImageColorAllocate'

等等,什么?好的,让我们检查一下。

# Let's sure the lib was found.
$ gcc -lgd_something gd.c
/usr/bin/ld: cannot find -lgd_something

# Lets sure we made no mistake with the symbol's name
$ nm /usr/lib/libgd.a
...
00000dc0 T gdImageColorAllocate
...
000003b0 T gdImageCreate

# So, everything should be ok
$ gcc -lgd gd.c
/tmp/cc6LReuX.o: In function `main':
gd2.c:(.text+0x19): undefined reference to `gdImageCreate'
gd2.c:(.text+0x31): undefined reference to `gdImageCreate'
gd2.c:(.text+0x59): undefined reference to `gdImageColorAllocate'
gd2.c:(.text+0x81): undefined reference to `gdImageColorAllocate'

$ echo $LD_LIBRARY_PATH
# Nothing

我不知道该怎么办。是 gcc 中的错误还是我做错了什么。在我以前的操作系统(Ubuntu 10.04)上一切正常。 我应该给你看哪个文件?

【问题讨论】:

标签: gcc linker linker-errors undefined-reference


【解决方案1】:

变化:

$ gcc -lgd gd.c

到:

$ gcc gd.c -lgd 

(原因:link order matters!)

哦,请在使用时添加 -Wall - 每次看到有人在禁用警告的情况下进行编译时,我都会感到非常痛苦。

$ gcc -Wall gd.c -lgd 

【讨论】:

  • 哇,它有效! gcc 的不明显特性。非常感谢。附言我总是用 -Wall,别担心 :)
  • 谢谢 - 这有助于缓解疼痛。 ;-)
猜你喜欢
  • 2013-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-18
  • 1970-01-01
  • 2023-03-08
相关资源
最近更新 更多