非常简单的程序的未定义引用

发布于 2024-12-20 00:15:12 字数 1574 浏览 3 评论 0原文

当我安装 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)上一切正常。 我应该向您展示哪个文件?

Once I installed Ubuntu 11.10, strange error appears. I want to use GD with my C program, so I installed package "libgd2-xpm-dev". Everything was installed - files gd.h and libgd.a are in "/usr/include" and in "/usr/lib". So, I've tried to compile simple program with 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'

Wait, what? Okay, let's check something.

# 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

And I don't know what shall I do. Is it an error in gcc or I do something wrong. On my previous os (Ubuntu 10.04) everything works well.
Which file should I show for you?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暖阳 2024-12-27 00:15:12

更改

$ gcc -lgd gd.c

为:(

$ gcc gd.c -lgd 

原因:链接顺序很重要 !)

哦,当你这样做时添加 -Wall - 每当我看到人们在禁用警告的情况下进行编译时,我都会感到非常痛苦。

$ gcc -Wall gd.c -lgd 

Change:

$ gcc -lgd gd.c

to:

$ gcc gd.c -lgd 

(Reason: link order matters !)

Oh, and add -Wall while you're at it - it pains me greatly every time I see people compiling with warnings disabled.

$ gcc -Wall gd.c -lgd 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文