Allegro.cc - Online Community

Allegro.cc Forums » Installation, Setup & Configuration » How to compile a program using allegro

This thread is locked; no one can reply to it. rss feed Print
How to compile a program using allegro
2D_rob
Member #15,095
May 2013

Hello,

I´m new to c programming and in the book i´m following I am to write a game which requires Allegro.

I'm sitting on a windows 7 box and use mingw to compile my programs. I installed allegro "allegro-5.0.8-mingw-4.6.2" and mingw "mingw-get-inst-20120426.exe" and to verify that it´s working I found this code:
#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;
}

I've been failing so many times trying to compile this code. this is what I write at the "MinGW Shell":
/e/cbin/proj/allegro
$ gcc -I/c/MinGW/include/allegro5 tst.c -o tst.exe
undefined reference to `al_install_system'
undefined reference to `al_create_display'
undefined reference to `al_map_rgb'
undefined reference to `al_clear_to_color'
undefined reference to `al_flip_display'
undefined reference to `al_rest'
undefined reference to `al_destroy_display'

/e/cbin/proj/allegro
$ gcc -l/c/MinGW/include/allegro5 tst.c -o tst.exe
c:/mingw/bin/../lib/gcc/mingw32/4.6.2/../../../../mingw32/bin/ld.exe: cannot find -lc:/MinGW/include/allegro5

I´ve tried a lot of other combinations but I can´t compile this one? Any ideas?

Gideon Weems
Member #3,925
October 2003

I'm not sure if answering this means I'm part of the solution or part of the problem (if you know what I mean), but...

Try adding "-lallegro" to the compile command.

2D_rob
Member #15,095
May 2013

Thanks for the reply!

Tried (in the "/e/cbin/proj/allegro" dir):
$ gcc -I/c/MinGW/include/allegro5 -L/c/MinGW/lib -lallegro tst.c -o tst.exe
c:/mingw/bin/../lib/gcc/mingw32/4.6.2/../../../../mingw32/bin/ld.exe: cannot find -lallegro
collect2: ld returned 1 exit status

Also this:
$ gcc -I/c/MinGW/include/allegro5 -L/c/MinGW/lib -lliballegro-5.0.8-mt tst.c -o tst.exe
c:/mingw/bin/../lib/gcc/mingw32/4.6.2/../../../../mingw32/bin/ld.exe: cannot find -lliballegro-5.0.8-mt
collect2: ld returned 1 exit status

Could I have missed some env variables?
User path says:
C:\MinGW\bin;C:\MinGW\msys\1.0\bin

LennyLen
Member #5,313
December 2004
avatar

Go to your MinGW\lib folder and look for anything that starts with liballegro. I can't tell you exactly what it will be named for your version of A5 as I built from the WIP source. If you want you can post any files you find here, and we can tell you which one specifically to link to.

edit: Just noticed from your last post... I'm guessing you have a liballegro-5.0.8-mt.a file. In which case, you link it with -lallegro-5.0.8-mt

2D_rob
Member #15,095
May 2013

This is what I have in the c:\mingw\lib

$ ls liballegro*
liballegro-5.0.8-md-debug.a
liballegro-5.0.8-md.a
liballegro-5.0.8-monolith-md-debug.a
liballegro-5.0.8-monolith-md.a
liballegro-5.0.8-monolith-mt-debug.a
liballegro-5.0.8-monolith-mt.a
liballegro-5.0.8-monolith-static-md-debug.a
liballegro-5.0.8-monolith-static-md.a
liballegro-5.0.8-monolith-static-mt-debug.a
liballegro-5.0.8-monolith-static-mt.a
liballegro-5.0.8-mt-debug.a
liballegro-5.0.8-mt.a
liballegro-5.0.8-static-md-debug.a
liballegro-5.0.8-static-md.a
liballegro-5.0.8-static-mt-debug.a
liballegro-5.0.8-static-mt.a
liballegro_acodec-5.0.8-md-debug.a
liballegro_acodec-5.0.8-md.a
liballegro_acodec-5.0.8-mt-debug.a
liballegro_acodec-5.0.8-mt.a
liballegro_acodec-5.0.8-static-md-debug.a
liballegro_acodec-5.0.8-static-md.a
liballegro_acodec-5.0.8-static-mt-debug.a
liballegro_acodec-5.0.8-static-mt.a
liballegro_audio-5.0.8-md-debug.a
liballegro_audio-5.0.8-md.a
liballegro_audio-5.0.8-mt-debug.a
liballegro_audio-5.0.8-mt.a
liballegro_audio-5.0.8-static-md-debug.a
liballegro_audio-5.0.8-static-md.a
liballegro_audio-5.0.8-static-mt-debug.a
liballegro_audio-5.0.8-static-mt.a
liballegro_color-5.0.8-md-debug.a
liballegro_color-5.0.8-md.a
liballegro_color-5.0.8-mt-debug.a
liballegro_color-5.0.8-mt.a
liballegro_color-5.0.8-static-md-debug.a
liballegro_color-5.0.8-static-md.a
liballegro_color-5.0.8-static-mt-debug.a
liballegro_color-5.0.8-static-mt.a
liballegro_dialog-5.0.8-md-debug.a
liballegro_dialog-5.0.8-md.a
liballegro_dialog-5.0.8-mt-debug.a
liballegro_dialog-5.0.8-mt.a
liballegro_dialog-5.0.8-static-md-debug.a
liballegro_dialog-5.0.8-static-md.a
liballegro_dialog-5.0.8-static-mt-debug.a
liballegro_dialog-5.0.8-static-mt.a
liballegro_font-5.0.8-md-debug.a
liballegro_font-5.0.8-md.a
liballegro_font-5.0.8-mt-debug.a
liballegro_font-5.0.8-mt.a
liballegro_font-5.0.8-static-md-debug.a
liballegro_font-5.0.8-static-md.a
liballegro_font-5.0.8-static-mt-debug.a
liballegro_font-5.0.8-static-mt.a
liballegro_image-5.0.8-md-debug.a
liballegro_image-5.0.8-md.a
liballegro_image-5.0.8-mt-debug.a
liballegro_image-5.0.8-mt.a
liballegro_image-5.0.8-static-md-debug.a
liballegro_image-5.0.8-static-md.a
liballegro_image-5.0.8-static-mt-debug.a
liballegro_image-5.0.8-static-mt.a
liballegro_memfile-5.0.8-md-debug.a
liballegro_memfile-5.0.8-md.a
liballegro_memfile-5.0.8-mt-debug.a
liballegro_memfile-5.0.8-mt.a
liballegro_memfile-5.0.8-static-md-debug.a
liballegro_memfile-5.0.8-static-md.a
liballegro_memfile-5.0.8-static-mt-debug.a
liballegro_memfile-5.0.8-static-mt.a
liballegro_physfs-5.0.8-md-debug.a
liballegro_physfs-5.0.8-md.a
liballegro_physfs-5.0.8-mt-debug.a
liballegro_physfs-5.0.8-mt.a
liballegro_physfs-5.0.8-static-md-debug.a
liballegro_physfs-5.0.8-static-md.a
liballegro_physfs-5.0.8-static-mt-debug.a
liballegro_physfs-5.0.8-static-mt.a
liballegro_primitives-5.0.8-md-debug.a
liballegro_primitives-5.0.8-md.a
liballegro_primitives-5.0.8-mt-debug.a
liballegro_primitives-5.0.8-mt.a
liballegro_primitives-5.0.8-static-md-debug.a
liballegro_primitives-5.0.8-static-md.a
liballegro_primitives-5.0.8-static-mt-debug.a
liballegro_primitives-5.0.8-static-mt.a
liballegro_ttf-5.0.8-md-debug.a
liballegro_ttf-5.0.8-md.a
liballegro_ttf-5.0.8-mt-debug.a
liballegro_ttf-5.0.8-mt.a
liballegro_ttf-5.0.8-static-md-debug.a
liballegro_ttf-5.0.8-static-md.a
liballegro_ttf-5.0.8-static-mt-debug.a
liballegro_ttf-5.0.8-static-mt.a

LennyLen
Member #5,313
December 2004
avatar

In that case then, yes, you want -lallegro-5.0.8-mt

edit:

I thought I'd add - this just links you to the base allegro library (mt as opposed to md means that the MS runtime libraries will be statically linked to your binary). Later on, if you start using the addons, you need to link these as well, or use the monolithic library, which contains al the addons in one.

2D_rob
Member #15,095
May 2013

Am I typing the gcc command wrong? The source file is located in e:\cbin\proj\allegro and is called "tst.c". When I run the command I am in that directory:

Is there something wrong with this line?
$ gcc -I/c/MinGW/include/allegro5 -L/c/MinGW/lib -lallegro-5.0.8-monolith-mt tst.c -o tst.exe

It shows these errors:
undefined reference to `al_install_system'
undefined reference to `al_create_display'
undefined reference to `al_map_rgb'
undefined reference to `al_clear_to_color'
undefined reference to `al_flip_display'
undefined reference to `al_rest'
undefined reference to `al_destroy_display'

LennyLen
Member #5,313
December 2004
avatar

If MinGW is set up correctly, you should only need to type:
gcc tst.c -lallegro-5.0.8-monolith-mt -o tst.exe

edit: Just for kicks, I downloaded the 5.0.8 binaries and tested it the same code and filenames you are using. The above command to gcc worked flawlessly for me.

2D_rob
Member #15,095
May 2013

Awesome! that did compile:
$ gcc tst.c -lallegro-5.0.8-monolith-mt -o tst.exe

$ tst.exe

When I ran the program I got an error popup saying:
"The program can´t start because.... blabla can´t find allegro-5.0.8-monolith-mt.dll"

I copied that dll from the location of the unzipped allegro bin folder to C:\MinGW\bin folder and ran the program again and I got no errors. So I´m guessing everything works fine.

There were a lot of DLL's in the unzip folder. Should I copy all of those to the C:\minGW\bin folder?

LennyLen
Member #5,313
December 2004
avatar

You can put them there. I prefer to copy them to each new project directory I make so that I know that all the files needed for the project (once it's built) are in one place.

2D_rob
Member #15,095
May 2013

Yeah that sounds smarter.

Thanks for all the help. Now I can finally get to the actual coding. :)

Cheers

LennyLen
Member #5,313
December 2004
avatar

Some people might say it's a waste of space having multiple copies of the same dll lying around, and this is true to some extent, but I have around 4Tb of unused HDD space, so I don't care. :D

MrJBRPG
Member #15,096
May 2013

I have a similar problem, but I am using C++ as my main language using allegro 5.0.7

Before I tell you the details, I will give you specifics

//////
Specifics:

Operating System: Mac OSX 10.6 Snow Leopard (Intel)
Programming environment: Terminal (UNIX)
I did not copy and paste the library files onto the directory containing the program.
//////

I have attempted to do the following steps from the previous post, but I get a series of undefined errors:

before using library calling

g++ -Wall -Wextra -o allegro_start allegro_start.cpp
Undefined symbols:
"_main", referenced from:
start in crt1.10.6.o
(maybe you meant: __al_mangled_main)
"_al_rest", referenced from:
__al_mangled_main in ccExARhf.o
"_al_install_system", referenced from:
__al_mangled_main in ccExARhf.o
"_al_create_display", referenced from:
__al_mangled_main in ccExARhf.o
"_al_flip_display", referenced from:
__al_mangled_main in ccExARhf.o
"_al_destroy_display", referenced from:
__al_mangled_main in ccExARhf.o
"_al_map_rgb", referenced from:
__al_mangled_main in ccExARhf.o
"_al_clear_to_color", referenced from:
__al_mangled_main in ccExARhf.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

I even tried for getting the main file to work

g++ allegro_start.cpp -Wall -Wextra -lallegro-5.0.7-main -o allegro_start
ld: library not found for -lallegro-5.0.7-main
collect2: ld returned 1 exit status

It's my first time using Allegro game engine too

Trent Gamblin
Member #261
April 2000
avatar

Question: do you have Allegro installed? If so where is it installed? If not, you have to build it from source on a Mac. Then you can link it and allegro_main depending on how its build. Usually something like -lallegro -lallegro_main.

MrJBRPG
Member #15,096
May 2013

Thank you, those were the two commands needed to correctly compile the program ;D, and I wonder what is the significance of -lallegro -lallegro_main?

i.e g++ allegro_start.cpp -Wall -Wextra -lallegro -lallegro_main -o allegro_start

Trent Gamblin
Member #261
April 2000
avatar

-lxyz tells the compiler to link with library xyz. The library has a bunch of functions and stuff just like a C source file and by linking it your program can use them.

Arthur Kalliokoski
Second in Command
February 2005
avatar

I might add that the stuff in libraries could be in source files in your project, but obviously linking all of Allegro each time you started a new project would be a pain. Libraries are kind of a systemwide collection of useful routines that are (hopefully) debugged, so you don't need the source for those functions in your project.

For instance, I find the Mersenne random number generator to be quite useful, so I made a library for it and stuck it in with the other local (to this computer) libraries such as Allegro, and put a mersenne.h in the local includes. It does break projects you give to other people though, since they most likely don't have such a library, so you have to include the source and header again.

“Throughout history, poverty is the normal condition of man. Advances which permit this norm to be exceeded — here and there, now and then — are the work of an extremely small minority, frequently despised, often condemned, and almost always opposed by all right-thinking people. Whenever this tiny minority is kept from creating, or (as sometimes happens) is driven out of a society, the people then slip back into abject poverty. This is known as "bad luck.”

― Robert A. Heinlein

Go to: