Saturday, July 09, 2011

Notes on using Allegro 5.1 on Mac

This is some notes I made whilst setting up Allegro for use on my Mac. I'm fairly new to Mac, having been a Windows programmer for a long time, so these notes may change as I learn more about the correct way to do things on the Mac.

Dependencies

Get all the libraries and tools that Allegro5 needs from Macport. This saves a lot of time. Very nice system. You can check what you have installed using:
port installed
You will need the following:
sudo port install zlib freetype jpeg libogg libvorbis libpng physfs +universal
If you have libraries installed that aren't 32 bit, and you get the "different system not i386" link error, they need to be recompiled with "+universal" (i.e. i386 and x86_64) . i386 is needed by the iPhone simulator. OSX desktop apps will use x86_64. You can force MacPorts to recompile any libraries using the following (and replace LIBNAME!):
sudo port upgrade --enforce-variants LIBNAME +universal
cmake should have been installed by MacPorts, but if not:
sudo port install cmake
Note: you will need at least version 2.8.8 of cmake if you are using Xcode 4.3 or newer.

Build 

make

Easiest way to build and install Allegro for OSX is use make. Get the Allegro source and in that directory:
  • mkdir build
  • cd build
  • cmake ..
  • make
  • sudo make install

Add "-D WANT_FRAMEWORKS=1" to cmake if you want frameworks.

Add "-D CMAKE_OSX_ARCHITECTURES=i386" if you want 32 bit libs. - I had to do this initially, I think because some of the ports wouldn't compile 64 bit. No longer necessary.

I don't use frameworks. Default install directory is "/usr/local".


Xcode

You can generate Xcode project files for Allegro and build using Xcode. I do two builds of Allegro. One for OSX and for iOS.

  • mkdir build_osx
  • cd build_osx
  • cmake -D SHARED=0 -G Xcode ..
  • xcodebuild -project ALLEGRO.xcodeproj -target ALL_BUILD -arch x86_64 -sdk macosx10.7 -configuration Release
  • cmake -D CMAKE_INSTALL_PREFIX=/my/install/path -P cmake_install.cmake 
I turn sharing off so I get static libs. You'll need to do this for iOS as dynamic libs aren't allowed.

Using

Don't use Xcode 4.0, it isn't ready. Use Xcode 4.1 or Xcode 3.2. Xcode 4.3 seems okay, still needs work. Stick with Xcode 3.2 if you like, I do on my laptop.

I create a Cocoa application and removed what I didn't want. If you followed make build instructions:
  • Add "/usr/local/include" to header search paths.
  • Drag in the Allegro libraries that you want to link to in "/usr/local/lib".
  • Go into Interface Builder and delete the default Application and Window. Allegro supplies these.

Linking


The frameworks you need to for OSX are:
  • AudioToolbox
  • IOKit
  • OpenAL
  • OpenGL
  • Cocoa

The frameworks you need to for iOS are:
  • CoreGraphics
  • Foundation
  • OpenAL
  • OpenGLES
  • QuartzCore
  • UIKit

The following error means you have the wrong "main" declaration:
dyld: Symbol not found: __al_mangled_main
Referenced from: /usr/local/lib/liballegro_main.5.1.dylib
Expected in: flat namespace
in /usr/local/lib/liballegro_main.5.1.dylib

It should be:
int main(int argc, char **argv) 

Other Info

There are other notes on the Allegro wiki on how to build Allegro from Git.


[16-Jul-11] Note about Xcode 4.0.
[27-Oct-11] Xcode 4.1 is fine. It doesn't crash and is faster than Xcode 3.2.
[Mar-12] Xcode 4.3. Compile 64-bit.
[29-Mar-12] Added +universal to "port install" so we get i386 and x86_64 libraries. I also updated the notes on the Allegro wiki.
[8-Jul-12] Added frameworks to link for Allegro 5.1.
[19-Dec-12] Notes on Xcode building.

5 comments:

Manderson said...

Wow, man you just saved me from banging my head against the -Wall (pun intended)...

I was simply trying out one of the tuts and couldn't figure out why I was getting that strange message! Wasn't even thinking to check the main func arguments.

Thanks for the helpful tips XD

Doug said...

Thank you very much, this just saved my day!

As a totally noob programmer on the mac, I find allegro to be kind of confusing. After _FINALLY_ getting it up and running, I got that error message in xcode, but your tip was right on.

Thank you!

Guilhermo said...

I'm getting this error when I try to run for iOS:
ld: library not found for -lallegro_color
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Any idea what this could be?

Emacs the Viking said...

Legend posting. I've played with SFML but found it *too C++-y* if that makes any sense.
I followed the OSX stuff to the letter, I use Mac Ports, it was flawless...there are quite a few compiler warnings but when all is said and done I have a set of headers and libraries in the right place.
Now to see if they work ;)
Many thanks for taking the time to post this information.

Chin Billy said...

Thanks for the feedback. I like to make notes when learning something, to remind me (!) and help others.

I've actually changed to using homebrew now. I like that it installs to the default UNIX directories, /usr/, rather than /opt/ for macports. Also you don't need to use sudo for that, and the packages seem to get updated more regularly. They are both good, I guess some is personal preference.