Undefined reference errors when using allegro

Aug 29, 2017 at 3:31pm
Hi everyone!

So I installed allegro today, but when I tried to run the sample code, it gave the following errors:


C:\Users\MICHI_~1\AppData\Local\Temp\ccd2nQPk.o	allegroDisplay.cpp:(.text+0x29): undefined reference to `al_install_system'
C:\Users\MICHI_~1\AppData\Local\Temp\ccd2nQPk.o	allegroDisplay.cpp:(.text+0x65): undefined reference to `al_create_display'
C:\Users\MICHI_~1\AppData\Local\Temp\ccd2nQPk.o	allegroDisplay.cpp:(.text+0xb0): undefined reference to `al_map_rgb'
C:\Users\MICHI_~1\AppData\Local\Temp\ccd2nQPk.o	allegroDisplay.cpp:(.text+0xcc): undefined reference to `al_clear_to_color'
C:\Users\MICHI_~1\AppData\Local\Temp\ccd2nQPk.o	allegroDisplay.cpp:(.text+0xd1): undefined reference to `al_flip_display'
C:\Users\MICHI_~1\AppData\Local\Temp\ccd2nQPk.o	allegroDisplay.cpp:(.text+0xe9): undefined reference to `al_rest'
C:\Users\MICHI_~1\AppData\Local\Temp\ccd2nQPk.o	allegroDisplay.cpp:(.text+0xf5): undefined reference to `al_destroy_display'
C:\Users\michi_000\Desktop\C++\andere\collect2.exe	[Error] ld returned 1 exit status


Here is the code I tried to run:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <cstdio>
#include <allegro5/allegro.h>

int main(int argc, char **argv)
{
   
   ALLEGRO_DISPLAY *display = NULL;

   if(!al_init()) {
      fprintf(stderr, "failed to initialize allegro!\n");
      return -1;
   }

   display = al_create_display(640, 480);
   if(!display) {
      fprintf(stderr, "failed to create display!\n");
      return -1;
   }

   al_clear_to_color(al_map_rgb(0,0,0));
   
   al_flip_display();

   al_rest(10.0);

   al_destroy_display(display);

   return 0;
}
Aug 29, 2017 at 3:38pm
Undefined reference means the linker cannot find the function you're trying to call. Not the compiler. The linker.

When trying to use functions from an external library (as you are), it means you haven't told it what libraries to link against.

You need to tell your linker what allegro libraries to link against. I'm guessing you're using windows. How are you building your code?
Aug 29, 2017 at 3:46pm
I'm using windows indeed. I'm using DevC++ and I just hit the compile button. In the compiler options I have added the allegro/bin to "binaries", allegro/lib to "libraries", and allegro/include to "C++ includes". I guess I have to add other directories too, but I have no clue which ones.
Last edited on Aug 29, 2017 at 3:57pm
Aug 29, 2017 at 4:29pm
It's not enough to tell it which directory the libraries are in. You also have to tell it which specific library files to link against.
Aug 29, 2017 at 5:05pm
So if I get this correct, I need to add all the files in my allegro/lib folder to the linker command?

Sorry for asking so many questions, but this is my first time using a 3rd party library and I'm having alot of trouble getting it to work.
Last edited on Aug 29, 2017 at 5:09pm
Topic archived. No new replies allowed.