Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined reference on very simple program

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?

like image 818
lanus Avatar asked Dec 07 '11 12:12

lanus


People also ask

How do you fix a undefined reference?

So when we try to assign it a value in the main function, the linker doesn't find the symbol and may result in an “unresolved external symbol” or “undefined reference”. The way to fix this error is to explicitly scope the variable using '::' outside the main before using it.

How do you fix undefined references in C++?

You can fix undefined reference in C++ by investigating the linker error messages and then providing the missing definition for the given symbols. Note that not all linker errors are undefined references, and the same programmer error does not cause all undefined reference errors.

How do you fix undefined symbol in C?

To allow undefined symbols, as in the previous example, use the link-editor's -z nodefs option to suppress the default error condition. Take care when using the -z nodefs option. If an unavailable symbol reference is required during the execution of a process, a fatal runtime relocation error occurs.

How do I fix linker error in C++?

You can fix the errors by including the source code file that contains the definitions as part of the compilation. Alternatively, you can pass . obj files or . lib files that contain the definitions to the linker.


1 Answers

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 
like image 113
Paul R Avatar answered Sep 21 '22 13:09

Paul R