Setting up MGTwitterEngine with YAJL 1.0.6 for iPhone development

*Corrected yajl include path. Updated info about adding YAJL static lib to project for deploying on device

MGTwitterEngine is a great Objective-C library for the Twitter API. Did you know the famous Twitterrific for iPhone (iTunes link) is built with MGTwitterEngine plus YAJL? You probably know that from the now famous YAJL error Twitterrific presented you when the Twitapocalypse hit the world, right?

But setting up MGTwitterEngine and YAJL for iPhone development turned out to be more difficult than I thought, due to the lack of documentation, and my lack of extensive experience with iPhone and Mac development. Anyway, after some hacking around, I finally got the two boys to play together and work with my next cool iPhone project. The following is how I did it and I think it might be useful to someone else wrestling with the same problem out there.

Getting and compiling YAJL on your Mac

First, get the latest YAJL code and get it compiled.

git clone git://github.com/lloyd/yajl

If you don’t have cmake on your Mac yet, get it via MacPorts. (The cmake version from Fink is too old to compile YAJL. I’ve tried it.)

sudo port install cmake

Now compile YAJL

cd yajl
sudo ./configure && make install

Now you should have “yajl-1.0.6” under the “build” folder. And the build process should have copied the binaries to /usr/local/lib/ and /usr/local/include/yajl respectively.

Getting MGTwitterEngine

Get the latest code from the SVN repo.

svn checkout http://svn.cocoasourcecode.com/MGTwitterEngine

Adding MGTwitterEngine to your iPhone project

Add everything from the MGTwitterEngine directory starting with “MGTwitter”, and also the NSString+UUID and NSData+Base64 category files, to the Xcode project. Choose to copy the files to the project.

Telling MGTwitterEngine to use YAJL

MGTwitterEngine doesn’t support Twitter’s search and trends API without YAJL, because these APIs only return JSON data. So obviously we need to tell MGTwitterEngine we have YAJL and please give us the support search and trends APIs, in addition to requesting JSON data for other Twitter API calls.

In MGTwitterEngineGlobalHeader.h


Adding YAJL and LibXML to the project

Now add the required frameworks to the Xcode project.

Locate “/usr/local/lib/libyajl.dylib”, drag and drop it onto “Frameworks” in the Groups & Files pane in Xcode. Don’t choose to copy the files.


Compiling YAJL from the source using the shipped makefile will produce the dynamic library binary files (yajl*.dylib) under /usr/local/lib. Although adding these files to the frameworks gets the project to compile and run on the simulator, this won’t work on a device since only static libraries are allowed on devices for third party frameworks.

Compiling the static library from the source code requires a bit more work but I found someone has already done that with more good stuff like an Obj-C wrapper for YAJL. So we can now simply grab this compiled YAJL static library for iPhone, exand it, then add the file “libYAJLIPhone.a” to the Frameworks.


Then also add “/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.0.sdk/usr/lib/libxml2.dylib” to Frameworks.

Next, open Project Info, locate the Build page, and look for the “Header Search Paths” property. Add the following two paths to the search paths property.


*Although it seems LibXML is not required, I did get compilation errors if LibXML is not in the Frameworks.

Hack and get MGTwitterEngine to build

It appears that MGTwitterEngine is still written against a previous version of YAJL (supposedly a pre-1.0 version). So if at this stage you go ahead and build the project, you will see two errors from MGTwitterEngine.

error: too few arguments to function 'yajl_alloc'
error: too few arguments to function 'yajl_free_error'

It looks like the latest version of YAJL has changed its signatures of the functions “yajl_alloc” and “yajl_free_error”. So let’s hack the corresponding code in MGTwitterYAJLParser.m to work around the build errors. Find the calls to these two functions and change the code as follows.

_handle = yajl_alloc(&callbacks, &cfg, nil, self);
yajl_free_error(nil, errorMessage);


Now you can hit Build and the project should build successfully. Next, you should take a look at AppController.h and AppController.m which come with MGTwitterEngine and see how to use it. It’s pretty easy to use once you get past the compilation problems. Now we have a fully functional and nicely built Twitter library ready to be used for any iPhone project! Also, if you have any suggestion or any better way to get the stuff to work please let me know.

  • Matt Donnelly

    THANK YOU SO MUCH!!!!!!!! This has been giving me headaches for days!

  • Dislay


  • Dan Morgan

    Oh my god I love you

  • Dan Morgan

    during the compile of YAJL I get this error:

    Scanning dependencies of target yajl
    CMake Error: Cannot open file for write: /Users/morgz/yajl/build/src/CMakeFiles/yajl.dir/depend.make.tmp
    CMake Error: : System Error: Permission denied

    :-( Looks like my YAJL celebrations were premature

  • http://damienh.org Damien

    Have you tried sudo'ing the commands? That usually gets the permission errors solved.

  • Dan Morgan

    Hey Damien – Thanks for the reply.

    I copied this directly into the console:

    sudo ./configure && make install

    Is sudo'ing when the command begins with the word sudo? I've had nothing but nightmares with YAJL!

  • Dan Morgan

    Hey Damien,

    Is there any chance you could uplaod the files so I could then copy them into /usr/local/lib/ and /usr/local/include/yajl manually?



  • http://fabian-kreiser.com/ fabiankr


    I added the search paths and it should work correctly, but it always ends saying it can't find the <yajl/yajl_parse.h> header…
    Don't know what I'm doing wrong here.

    Help appreciated. 😉

  • Alex K

    I was having a similar problem. I noticed that in the instructions here, it says to include /usr/local/lib/yajl in the header search path; however, on my machine, it installed the yajl headers to /usr/local/include/yajl. That might fix it for you. (It didn't for me — I had to copy the headers in that directory directly into Xcode and then modify how they referred to each other — changing <yajl/yajl_common.h> to “yajl_common.h”, for instance.)

  • peterxu

    The way I solved this problem is that I made the folder
    /usr/local/lib and /usr/local/include “read and write” from “read only” in the finder. (use “go to folder” in finder and type the address)

    Hope that helps!

    btw: if you are developing for iphone OS 3.0 as I did, you might get error with [NSApp terminate:self] in the delegate file. I used “exist (0)” instead. I wander if anyone has the same problem

    Also can anyone inform me how to get the status data? I tried:
    NSMutableArray *user1= [message objectAtIndex:0];
    in the statusesReceived method but get the NSCFArray beyond bounds error.

  • http://en-gb.facebook.com/jaredholdcroft Jared Holdcroft

    I had this exact problem but changing the header search path to be just /usr/local/include fixed it. If you don't it expects yajl/yajl_parse.h to be in /usr/local/include/yajl/yajl!

  • http://www.facebook.com/houyr Yong Rong Hou

    You are right. The include path should (of course) be /usr/local/include/yajl. That was my mistake. just corrected it in the article.

  • thnk2wn

    Did you get around this problem? I've tried changing the folder permissions of yajl/build/src/CMakeFiles/yajl.dir but somehow it keeps getting set back to readonly.

  • Dan Morgan

    No :-( I also set the permissions and I still cant sudo the files into them. I'm gonna try and manually copy the files from the YAJL folder into the respective usr folders. Will let you know how I get on.

  • thnk2wn

    In the end that part didn't end up mattering in my case. I didn't see his update regarding the compiled YAJL static library for iPhone and libYAJLIPhone.a. I was able to just use that and copy the YAJL header files to the include directories and set the project search paths. I haven't tested out the code but I have it compiling now.

  • Dan Morgan

    Thnk2wn. Would you be as kind to upload an example of the proj? I’ve been struggling with this for soo long now and I’m still getting 44 errors

  • 匿名

    See http://files.getdropbox.com/u/235508/iPhone/MGTwitterTemp.zip but note that (a) this is from a stripped down copy of my app and (b) it may not be at this location long. Also you’ll still need the include files (usr/local/include) etc.

  • Dan Morgan

    Thanks you so so much. I’ve tested this and it works perfectly. I just had to change the header search path for the libxml2 to include a final ‘/’. Fantastic, you have no idea how long this has been doing my nut in for.

  • http://twitter.com/jessearmand Jesse Armand

    I decided to do what gabriel did on yajl-objc, which is by including all of the yajl source code in the project, instead relying on that static library, because I also need this for iPhone OS 2.x.

  • http://mowglii.com/ Mowglii

    instead of doing:

    sudo ./configure && make install

    break it up into two commands:

    sudo ./configure
    sudo make install

  • intomo

    I’ve followed your suggestions above, but regardless of which search path I add it still won’t compile. I still get an error about it can’t find “yajl_parse.h” and 28 other dependant errors. Questions: I’m new to Mac so exactly where is /user/local/ on my drive? When I Go To Folder in the finder it says that folder doesn’t exist. Do I need to add the yajl files any place physically, or just add the framework via the .a file? Do I need to add the class files in that folder to my classes? Many thanks in advance!

  • http://programmershotel.com joe_carney

    I’ve followed your instructions and now when I compile, I get this…

    Library not found for -lyajl

    Command /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/gcc-4.2 failed with exit code 1

    Can’t find anything on the Internet to help me out. I’d really like to use this but it has been an absolute nightmare getting started with it…

    Any ideas?

  • dev

    Thanks for this tip!

  • dev

    Thanks for this tip!

  • http://twitter.com/parimalsatyal Parimal Satyal

    Thanks so much for this article. After going through it and, I’ll admit, encountering a whole lot of errors, mostly as a result of my unfamiliarity with Obj-C, JSON, XML and the SDK, it finally works!

    In my case, I had to put in “/usr/local/include/” (without the ‘yajl’ sub-directory) as a Header Search Path to get it to work. Wouldn’t see the file otherwise. (This took a while for me to figure out :P)

    A tip with adding the libxml2.dylib framework to the project:
    Instead of using Finder and dragging it in, you can also command-click Resources › Add › Existing Frameworks, and chose libxml2.dylib

    I could previously only find “libxml2.2.dylib”, and this doesn’t seem to work very well.


  • http://twitter.com/DrKdev Shawn Koppenhoefer

    Adding /usr/local/include/yajl to the “Header Search Paths” property didn’t work for me. Compiling still gave the error ‘yajl/yajl_parse.h: No such file or directory’.

    My solution to the failed yajl header include was the following:

    Instead of adding /usr/local/include/yajl to Xcode’s “Header Search Paths” property, I just added /usr/local/include
    BINGO.. problem solved.


    p.s. My compile was failing with the message ‘yajl/yajl_parse.h: No such file or directory’ and yet /usr/local/yajl/yajl_parse.h WAS present (and readable) and the Header Search Path *did* specify /usr/local/include/yajl.

    When I ran ‘sudo dtrace -s /dev/stdin | grep yajl’
    and then typed:

    printf(“%s %s”, execname, copyinstr(arg0));

    followed by Enter
    followed by Control-D
    followed by recompiling in Xcode… (oof!)

    I saw that Xcode was trying to open /usr/local/include/yajl/yajl/yajl_parse.h.
    So I just removed the extra yajl in the “Header Search Path”.


  • http://twitter.com/je_d_h JeDongHyun

    Thank you so so so so so so so so * 100 You saved my brain! :-)

  • http://sean808080.com sean808080

    ok. i hated you at first for suggesting I through with all of this but now I love you because it works. :-)

  • kanyal

    HI Guys
    i have set header search path and i see files are there in /usr/local/include/yajl but still it give error yajl/yajl_parser.h no file or directory found.
    i have changed it to just yajl_parser.h but no success. can any body help what i am still missing.

  • http://twitter.com/Gonzo_Oin Gonzo Oin

    Thank you so much !

  • threadpool

    I had following errors
    “.obj_class_name_MGTwitterMessagesYAJLParser”, referenced from literal-point@_OBJC@__cls_refs@MGTwitterMessagesYAJLParse in MGTwitterEngine.o

    I add already the libYAJLIPhone.a static lib to Frameworks.

    What's wrong??

  • Mailtosubrat

    Its great ……
    i like it

  • Nathan H Jones

    Hey Damien – I've been working through issue after issue with MGTwitter and then stumbled across your blog. Great post and it got me from 23 errors (which I had already got down from 163) to 5. However, after implementing the hack in your final step, it throws the final set of errors pasted below – they all seem to reference the same initWithJSON method.

    1) _yajl_alloc referenced from…
    2) _yajl_free_error referenced from…
    3) _yajl_parse referenced from…
    4) _yajl_get_error referenced from…
    5) _yajl_free referenced from…

    …initWithJSON:theJSON delegate:theDelegate
    connectionIdentifier:theIdentifier requestType:reqType

    located in MGTwitterYAJLParser.m

    Any help would be REALLY appreciated!!


  • Thinksecret

    I’ve read below people are getting errors when running sudo ./configure && make install

    This may cause issues so try this instead: sudo ./configure && sudo make install

  • Grateful

    Pure win. Thank you so much!

  • Adamgf

    What you should probably do is ./configure && sudo make install

  • tom

    Nathan, did you end up solving this issue? I have exactly the same problem.

  • Walid

    In your build target, you need to exclude all the files whose names contain “YAJL”.If you’re using Xcode 4, this should be accessible in your target’s Build Phases, under “Compile Sources”.