未定义的非常简单的程序参考 [英] Undefined reference on very simple program

查看:164
本文介绍了未定义的非常简单的程序参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我不知道该怎么做。这是gcc中的错误还是我做错了什么。在我之前的操作系统(Ubuntu 10.04)上,一切运行良好。
我应该为您显示哪些文件?

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?

推荐答案

更改:

$ gcc -lgd gd.c

$ gcc gd.c -lgd 

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

(Reason: link order matters !)

哦,然后添加 -Wall - 每当我看到人们编译时警告被禁用,它都会让我非常痛苦。

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 

这篇关于未定义的非常简单的程序参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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