Pages

Friday, January 14, 2011

Compiling Allegro 5 with Mac OS X Snow Leopard (10.6.6)

This tutorial takes you through building Allegro 5.0.0rc4 and compiling your first Allegro program. You will need the following:

The steps of the tutorial are as follows: 
  1. Extract Allegro source files, and install CMake.
  2. Use the CMake GUI to specify the Clang compiler, and to generate the makefile.
  3. Run make and make install at the command-line.
  4. Write and run your first Allegro 5 program.
Extract Allegro source and install CMake

  1. Open Terminal.
  2. cd ~/Downloads/
  3. tar xvzf allegro-5.0.0rc4.tar.gz
  4. cd allegro-5.0.0rc4.tar.gz
  5. mkdir Build
  6. cd Build
  7. Double-click on the cmake .dmg file and install CMake.
The cd command, change directory, takes you inside the Downloads/ folder. The command tar with the x option specifies an extraction operation. (tar can also list, add or create compressed files.) The additional options v, z, f specify the following, respectively: 
  • Show me what you're extracting.
  • z because it's a gzip file. Notice the filename has a .gz extension.
  • f because I'll supply you with the filename.
The Allegro README recommends building in a separate build directory, so we make the directory with mkdir. If you mess up during the build process, you just have to delete one folder; it's cleaner. We then go into the Build/ directory so that everything is built in this location. 

Installing CMake from a .dmg file should be self-explanatory.

Specify Clang as our compiler and generate the Makefile
  1. Launch the CMake application from your Applications folder. 
  2. In the Source field, enter /Users/your-login-name/Downloads/allegro-5.0.0rc4
  3. In the Build field, enter /Users/your-login-name/Downloads/allegro-5.0.0rc4/Build
  4. Click Generate - and when you do, a dialog like this should appear: 

If this dialog does not appear, make sure your Build/ directory is empty. This dialog needs to appear in order for you to specify Clang ("cee-lang") as your compiler of choice. It has to do with gcc being the default compiler and Snow Leopard being a 64-bit OS.

When this dialog appears, be sure to select "Specify native compilers." Clang comes with Snow Leopard; we just need to tell CMake that we will use Clang as our C compiler instead of the GNU C compiler. Click Continue. 

In the next dialog, in the C text field, enter the path to the clang tool: /Developer/usr/bin/clang


Click Done, and CMake should create nine files in the build directory. You can use ls to list them.


Make and make install

  1. Still in the Build/ directory, type sudo cmake .
  2. sudo make
  3. sudo make install
Alright, now take a look at the /usr/local/lib directory. Note the pattern of each filename:
  • lib
  • allegro or allegro_something
  • .dylib
Write and run your first Allegro program
  1. Create a text file alleg5test.c and enter this code listing.
  2. gcc alleg5test.c -o alleg5test -L/usr/local/lib -lallegro -lallegro_main
  3. ./alleg5test

You should get a window with a black background to show up. If you did, congratulations!

Check out the pattern of the flags: a -l ("dash-el") followed by allegro_. I guess all of them are the .dylib files from /usr/local/lib. Hmm.

You might be able to adjust this tutorial to work with Xcode, and then to create an Allegro template. (Hint: See my previous tutorial on Flixel.)

I hope this saves you some time. Go through the tutorials, and go make some games!

References

Updates

  • 1/15/2011: Removed -lz option from the long gcc above. That would have told the linker to look for a library named "z." Lol.
  • 1/15/2011: Much shorter gcc invocation. Originally gcc alleg5test.c -o alleg5test -lallegro -lallegro_font -lallegro_main -lallegro_primitives -lallegro_image -framework AppKit -framework OpenGL /usr/lib/libIOKit.dylib -framework Cocoa, but not all of these are needed (at least for our small program). Compare with Allegro 4.4, where you did gcc helloworld.c `allegro-config --libs`. The output of allegro-config 4.4.1 was -L/usr/local/lib -framework Cocoa -lalleg-main -lalleg. Similar, no? 
  • 1/15/2011: I can't compile the bitmap tutorial at the Allegro wiki. My error message is, "Undefined symbols: _al_set_clear_to_color." Strange. Oops, typo. gg. There's a al_clear_to_color() function; I was just practicing my speed typing.
  • 1/15/2011: Added an extra step, sudo cmake ., because there's no Makefile after CMake creates its nine files.
  • 1/22/2011: Took out the "-framework Cocoa" switch in gcc. Don't need it.
Now, if only there was a allegro-config that could do for Allegro 5 what it did for Allegro 4.4. (If you're up to it, write a program allegro5-config that outputs the appropriate arguments to gcc, place it in /usr/local/bin, and then try gcc alleg5test.c `allegro5-config [--libs]`. It might not even have to be a program, but just a shell script.)

4 comments:

  1. Best tutorial I've seen for this. Was looking for days for a quick way of getting setup on eclipse for OSX. Found this and was running in about 5 minutes. Couldn't believe it.

    You might add a section for eclipse as well. The only additions are where you setup the compiler itself.

    Create a new C++ project in the eclipse CDT variant. Right click the project and go to Properties, C/C++ Build, Settings. Over to the right, there will be a new list that has an entry related to the C++ Linker. On my machine it was MacOS X C++ Linker, but it varies per system. Choose Libraries under that. Two frames will appear to the right of that with the names "Libraries (-l)" and "Library search path (-L)".
    In the first, add "allegro" and "allegro_main". In the second, add "/usr/local/lib".

    I also think you should post this directly to the allegro wiki. It's quite useful, and their docs are lacking for the Mac.

    ReplyDelete
  2. Hi,

    Thank-you for the Eclipse setup notes.

    I think it'd be cool to have it on the Allegro wiki. If I come around to Allegro in the future, I would like to put it up there.

    I'm glad people are getting good stuff out of this tutorial. :)

    Thank-you for the comment,
    Coop

    ReplyDelete
  3. Any tips on how to install in Xcode? I have been trying to install for over a week. I THINK I have it finally installed but my little hello world program which doesn't even invoke Allegro spitting out "can't find this"...

    ReplyDelete
  4. Hi Michael,

    Using Allegro with Xcode is like using flixel with Xcode: you tell Xcode what settings are passed to a compiler. This is called "using an external build tool." For Allegro, it is gcc. For flixel, it is mxmlc.

    I wrote a tutorial on setting up flixel here: http://partitionseven.blogspot.com/2010/10/flex-flixel-and-xcode.html

    From that link, step 3 might be the most relevant. I'm guessing in place of mxmlc, you would use gcc. Be sure to specify the -L and -l options.

    I currently do not own a Mac. I hope that the link helps. If you find a way to make Allegro 5 work in Xcode, please include a link to the template and to your steps. It will be useful to a lot of folks.

    Sorry for taking so long to reply.

    Thank-you,
    Coop

    ReplyDelete