非常简单的程序的未定义引用
当我安装 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更改
为:(
原因:链接顺序很重要 !)
哦,当你这样做时添加
-Wall
- 每当我看到人们在禁用警告的情况下进行编译时,我都会感到非常痛苦。Change:
to:
(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.