2

I tried installing the Allegro library today. I have same experience in C++, but it seems I have none in doing stuff like that. I have compiled Allegro 5.0 from source and put it in /usr/lib/gcc/i486-linux-gnu/4.4/include/allegro5. But when I try to compile my code, this comes up:

    > g++ test2.cc -o test2
/home/chris/Desktop/c++/test2/.objs/main.o||In function `main':|
main.cpp:(.text+0x22)||undefined reference to `al_install_system'|
main.cpp:(.text+0x3e)||undefined reference to `al_create_display'|
main.cpp:(.text+0x6b)||undefined reference to `al_map_rgb'|
main.cpp:(.text+0x8e)||undefined reference to `al_clear_to_color'|
main.cpp:(.text+0x93)||undefined reference to `al_flip_display'|
main.cpp:(.text+0xa1)||undefined reference to `al_rest'|
main.cpp:(.text+0xa9)||undefined reference to `al_destroy_display'|
||=== Build finished: 7 errors, 0 warnings ===|

The code I am using:

#include <stdio.h>
#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;
}

allegro-config --libs returns nothing. I had Allegro as package first, but it didn'T work either.

Suyooo
  • 75
  • 1
  • 2
  • 7
  • /usr/lib/gcc/i486-linux-gnu/4.4/include/allegro5 is not the right place to put library's include files. – milan Mar 09 '11 at 08:53
  • possible duplicate of [Compiling C++ code with allegro 5 and g++.](http://stackoverflow.com/questions/6377204/compiling-c-code-with-allegro-5-and-g) – karlphillip Jun 16 '11 at 20:22

2 Answers2

21

Allegro 5 uses pkg-config.

gcc foo.c -o foo $(pkg-config --libs allegro-5.0)

You will also need to specify addon libraries that you use:

gcc foo.c -o foo $(pkg-config --libs allegro-5.0 allegro_image-5.0)
Matthew
  • 47,584
  • 11
  • 86
  • 98
  • After running 'pkg-config --list-all | grep allegro', I realized that I had to omit the dot zero for the command to work for me: 'gcc allegro_play.c -o play $(pkg-config --libs allegro-5)' – HappyCoder86 Nov 06 '15 at 20:29
1

You need to link with the Allegro libraries, but you don't seem to do so.

allegro-config --libs returns nothing.

There's part of your problem. Normally, you should be able to add it to your command line, like so:

g++ `allegro-config --libs` test2.cc -o test2

On my system (Ubuntu 10.10) it gives this:

$ allegro-config --libs
-L/usr/lib -Wl,-Bsymbolic-functions -lalleg-4.2.2

Are you invoking the right allegro-config? Maybe you should specify the full path to it?

Thomas
  • 174,939
  • 50
  • 355
  • 478
  • As I said, I haven't installed Allegro by packages, but rather compiled it from source. When I had the package installed, it returned what you posted above. Should I try deleting Allegro5 and installing the packages again or would that change nothing? – Suyooo Mar 07 '11 at 17:04
  • I repeat the two questions above. – Thomas Mar 07 '11 at 17:05
  • With nothing I meant it's not installed, sorry if that was unclear. I tried reinstalling the packages (liballegro-4.2, liballegro-4.2-dev). The command now outputs the same as on your Ubuntu, and invoking the right still leads to the same output as in the OP. – Suyooo Mar 07 '11 at 17:14
  • The Ubuntu packages give you Allegro 4, which should not even let you compile the code from the OP, let alone link it. Either way, you could also manually add `-L/path/to/your/allegro/libs -lalleg` or something like that. – Thomas Mar 07 '11 at 17:20
  • Oh, haha, seems like I'm stupid :I An Allegro 4 code works fine - if Allegro 5 doesn't work yet, I'll just start with this first. Thanks for coping with my stupidity :p – Suyooo Mar 07 '11 at 18:25
  • @Suyo, Allegro 5 works fine. Note that it is a complete rewrite from version 4, and the build process is different as well. – Matthew Mar 08 '11 at 22:18
  • I think the real answer is here: http://stackoverflow.com/questions/6377204/compiling-c-code-with-allegro-5-and-g/6377275#6377275 – karlphillip Jun 16 '11 at 20:16