Allegro.cc - Online Community

Allegro.cc Forums » Installation, Setup & Configuration » How to compile an Allegro 5 program?

This thread is locked; no one can reply to it. rss feed Print
How to compile an Allegro 5 program?
David Pavezka
Member #17,392
January 2020

Hi, I'm very very sorry, but I'm a novice in Allegro (and C programming in general), and I haven't found a good answer to my problem online, so I'll ask here, at a risk of coming off as a giant noob (that I probably am :-/ ):
After following the quickstart guide here, I've encountered an unexpected problem of not being able to compile hello.c using MinGW. The command supplied:

gcc hello.c -o hello.exe $(pkg-config allegro-5 allegro_font-5 --libs --cflags)

does not work, and I'm not sure how to link the libraries using -l commands. I'm almost certain that I've configured the library correctly (following the guide).
I'd simply like to know how to successfully compile the code using cmd and MinGW. I've seen somthings about CMake, but I have absolutely no idea how to use it, and if possible, avoid it (at least until I'll understand what it does).

The command above produces:

gcc: error: $(pkg-config: No such file or directory
gcc: error: allegro-5: No such file or directory
gcc: error: allegro_font-5: No such file or directory
gcc: error: unrecognized command line option '--libs'
gcc: error: unrecognized command line option '--cflags)'

The command above, but without the dollar sign and stuff in parenthesis (gcc hello.c -o hello.exe) produces:

c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\DAWIDP~1\AppData\Local\Temp\cceUBMFy.o:hello.c:(.text+0x21): undefined reference to `al_install_system'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\DAWIDP~1\AppData\Local\Temp\cceUBMFy.o:hello.c:(.text+0x26): undefined reference to `al_install_keyboard'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\DAWIDP~1\AppData\Local\Temp\cceUBMFy.o:hello.c:(.text+0x34): undefined reference to `al_create_timer'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\DAWIDP~1\AppData\Local\Temp\cceUBMFy.o:hello.c:(.text+0x40): undefined reference to `al_create_event_queue'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\DAWIDP~1\AppData\Local\Temp\cceUBMFy.o:hello.c:(.text+0x5b): undefined reference to `al_create_display'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\DAWIDP~1\AppData\Local\Temp\cceUBMFy.o:hello.c:(.text+0x67): undefined reference to `al_create_builtin_font'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\DAWIDP~1\AppData\Local\Temp\cceUBMFy.o:hello.c:(.text+0x73): undefined reference to `al_get_keyboard_event_source'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\DAWIDP~1\AppData\Local\Temp\cceUBMFy.o:hello.c:(.text+0x86): undefined reference to `al_register_event_source'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\DAWIDP~1\AppData\Local\Temp\cceUBMFy.o:hello.c:(.text+0x95): undefined reference to `al_get_display_event_source'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\DAWIDP~1\AppData\Local\Temp\cceUBMFy.o:hello.c:(.text+0xa8): undefined reference to `al_register_event_source'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\DAWIDP~1\AppData\Local\Temp\cceUBMFy.o:hello.c:(.text+0xb7): undefined reference to `al_get_timer_event_source'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\DAWIDP~1\AppData\Local\Temp\cceUBMFy.o:hello.c:(.text+0xca): undefined reference to `al_register_event_source'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\DAWIDP~1\AppData\Local\Temp\cceUBMFy.o:hello.c:(.text+0xe1): undefined reference to `al_start_timer'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\DAWIDP~1\AppData\Local\Temp\cceUBMFy.o:hello.c:(.text+0xf8): undefined reference to `al_wait_for_event'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\DAWIDP~1\AppData\Local\Temp\cceUBMFy.o:hello.c:(.text+0x13e): undefined reference to `al_is_event_queue_empty'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\DAWIDP~1\AppData\Local\Temp\cceUBMFy.o:hello.c:(.text+0x166): undefined reference to `al_map_rgb'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\DAWIDP~1\AppData\Local\Temp\cceUBMFy.o:hello.c:(.text+0x193): undefined reference to `al_clear_to_color'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\DAWIDP~1\AppData\Local\Temp\cceUBMFy.o:hello.c:(.text+0x1ba): undefined reference to `al_map_rgb'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\DAWIDP~1\AppData\Local\Temp\cceUBMFy.o:hello.c:(.text+0x211): undefined reference to `al_draw_text'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\DAWIDP~1\AppData\Local\Temp\cceUBMFy.o:hello.c:(.text+0x216): undefined reference to `al_flip_display'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\DAWIDP~1\AppData\Local\Temp\cceUBMFy.o:hello.c:(.text+0x232): undefined reference to `al_destroy_font'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\DAWIDP~1\AppData\Local\Temp\cceUBMFy.o:hello.c:(.text+0x241): undefined reference to `al_destroy_display'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\DAWIDP~1\AppData\Local\Temp\cceUBMFy.o:hello.c:(.text+0x250): undefined reference to `al_destroy_timer'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\DAWIDP~1\AppData\Local\Temp\cceUBMFy.o:hello.c:(.text+0x25f): undefined reference to `al_destroy_event_queue'

Any help would be appreciated ;D.

Edgar Reynaldo
Member #8,592
May 2007
avatar

You don't use pkg-config on Windows.

Just link directly with the allegro libraries.

Did you download binaries? Or did you build allegro yourself?

Are you using the monolith? Or the addon libraries?

You need to link to each one you use.

They come in the 'lib' folder.

David Pavezka
Member #17,392
January 2020

1. I downloaded this release from github: allegro-x86_64-w64-mingw32-gcc-8.2.1-posix-seh-static-5.2.5.1.zip
2. I'm using the libraries supplied with the download, not sure what libraries are monolithic or addon,
3. What command should I use to link the libraries? Should I just pass all 56 .a files with -l? How can I determine which ones to use?

I tried using, gcc hello.c -lallegro-static -lallegro_font-static -o hello.exe, but to no avail.
Once again, sorry for not being able to find answers on web.

Edgar Reynaldo
Member #8,592
May 2007
avatar

Okay. We'll get this sorted.

1) You're using 64 bit MinGW-W64 GCC 8.2 binaries. Do they match the compiler you're using?

At a command prompt, enter 'gcc --version' with mingw\bin on your path.

2) The libraries are labeled properly. Just read the names. Either there will be a monolith or there will be addons or there will be both.

3) You need to link to each library you use. The monolith library is provided as a 'shortcut' to linking to every allegro addon library, including the main allegro library. Determine which you're using first, and then link to them.

-lNAME where the file is named libNAME.a is the command line option you use to link.

4) You can link either dynamically (and depend on .dll files) or statically. Choose one.

David Pavezka
Member #17,392
January 2020

1. My version of GCC is 8.2.0-5; I know it's not 8.2.1, but I hope it's close enough, since there's no 8.2.0 build and I know building the library myself may be tiresome. Also, I just now noticed that allegro builds are only 64 bit, but AFAIK MinGW is only offering GCC in 32bit format (I'm running Windows 10 64bit). Am I missing something?

2&3&4. The program uses only the allegro5.h and allegro_font.h. I'm pretty sure I want to compile statically (so no .dll, right?) so I tried using -lallegro-static -lallegro_font-static, but it's the same error. Same with -lallegro_monolith-static.

So is the cause the difference in bits? Cause if so, since MinGW is only 32 bit (AFAIK) do I have to compile the library myself?

Edit: See, this is why you shouldn't live under a rock, cause it turned out there is a 64 MinGW that I had no idea about. I'll check it out and report later. Also, I just found out that i686 means 32-bit, so... uh... yeah...

Edgar Reynaldo
Member #8,592
May 2007
avatar

David Pavezka
Member #17,392
January 2020

Ok, at first it threw an error about missing libraries; I get those, and the same error occurs! ::) I'll try using mingw-w64 now, maybe this'll help...
Edit: I dunno, now I get some weird errors... Oh well, I'll work on it tomorrow when I'll wake up. Thanks for help anyway. ;D

Edgar Reynaldo
Member #8,592
May 2007
avatar

David Pavezka,

You probably forgot to add the search directory for the linker. You need to pass the path to where the libraries are so it can find them.

-L path\to\libs

If it still isn't working, post the errors that you get. If it's long, use pastebin.

bamccaig
Member #7,536
July 2006
avatar

No need to use a pastebin when posting large output on Allegro.cc. The Web is not IRC. :P Just post them here. It'll be fine. I doubt the output could be any larger than a megabyte or so. Of course, you could also put the output into a file and attach it.

On a side note, I get the feeling that Edgar may be assuming a certain level of knowledge/experience that you may not meet. If you feel you don't understand the advice you're given that's OK. Just say so.

There's also the old adage that if you teach a man to fish they'll eat for life. It's great to find out what you're missing, but it's even better to understand what's happening so you can figure it out for yourself next time. If you have any questions about why you need to do something ask.

Go to: