Link Freetype to your project statically in Xcode.

Linking FreeType to your project in Xcode for an OpenGL program.

So, fancy using text in your OpenGL code `eh? 

Anyone who has tried to render text in OpenGL will tell you, its not a piece of cake, you will have to pass some hurdles. They might also tell you, the best way to go about it is to use default text and font rendering, but you don’t want that ‘un’-anti-aliased text, what you’re looking for is a text which looks good, is anti-aliased, and can load any glyph from TTF file. Don’t know if this is the case for you, but I certainly wanted an anti-aliased text, if not a TTFed glyph. 
{you know who THEY are :P}

So, after searching a bit on internet and banging my head a bit on stackoverflow, I found out that there is this library called FreeType, which can render text, and is able to load any .ttf file you send to it. I downloaded it, went through the install notes.

Somehow, i was able to install it in my Xcode project using .dylib files that were created during make install, so I copied these files into my project source and gave appropriate paths, but what you know, when I created an archive it was still looking for dylib in the path where it was installed. I found out this when I deployed my project archive on clients machine. 

The archive failed with dylib NOT LOADED, reason: image not found, being new to mac development I didn’t know what was happening, so, after looking up on stackoverflow again I found out that relative path to the archive can be given using install_name_tool command on mac, so I used it, and replaced those paths, and deleted the freetype installation from my machine, just so I could test it.
It worked fine on my machine, [as it always does] but when I deployed the same archive to the client side, it failed, but there were no dylib issues this time. The problems were in the functions called from this dylib. It was EXC_BAD_INSTRUCTION. [btw, i am still looking for an ans to this :) ]
I couldn’t really found why it was happening, so I decided to link the freetype library statically. [with .a file]
Some people say you’ll have to create a workspace and stuff in Xcode in order to do this… i didn’t find any point in doing that so i simply included the static libraries in my Xcode and gave the right path.

Well, let’s get started with the steps on how to do that right away..

  1. compile freetype, make sure it generated .a file. [you should be able to find this on lib folder inside your ft home directory]
  2. copy .a file in your project dir if you want to run your project on other mac.
  3. Now you need to add this .a file to your project in Xcode, for that you go to build phrases in target and add your .a file to it.
    PS: the .a file is called libfreetype.a
  4. Next up you need to set flags for freetype, you can do this in other linker flags, the flags you need for FT are, -lfreetype -lz -lbz2
  5. Now you can build your project…. and BAMMM!!! what happened there? FT never works alone, like many police movies from Hollywood it always works with a partner, this time the partner being libpng.
  6. If you have libpng, well and good, just simply copy the .a file of libpng along with libfreetype.a file, and you’re good to go., else download and install libpng, or if you prefer a more geeky way, make/make install do that.
  7. And now build and run you’re project.

You will find that all your troubles have gone away :)  

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s