Yet More Reasons for Box2D Compilation Issues

And a one-shot, elegant solution.

More reasons for Box2D projects not compiling mostly boil down to a) various C++ vs. Objective C conventions and practices and b) build settings.  If you are getting zillions of compile issues from a project, especially a tutorial or sample you pulled down from the web (and must have worked for someone, somewhere, once), the major culprits are often:

  • wrong version of Box2D.  Cocos2d and Box2D and all the various open source and 3rd party supplied tools you might use to “leverage” your productivity bring with them varying degrees of stability and varying practices with regard to “backward compatibility”.  My initial impression is that, for most of the tools I am looking at or using, they are too new to be worried (yet?) about “installed base” and inconvenience for users (who are all, for the most part, using “free” stuff, I should note.)  The wrong version may manifest itself with different API’s — different method names, different arguments, different assumptions about data types both internal and external (e.g. in a particular version of a package it is integrated with, but for which you have already installed a newer or different version — probably to work with some other project!). [I think there is a small point here related to how “free” open source stuff can turnout to be in some circumstances…]
  • Objective C vs. C++ conventions.  Where [ #import “constants.h” ] might compare in your brain to be congruent to [ #import <constants.h> ], look closely — they are different.  It is easier to spot when you see these forms of compiler directives mixed together in the same code module — as they are in Cocos2DS and elsewhere.  So this should lead to a check of “compiler directives” in your build settings.  This should be the first thing you think of when you are getting “include file not found” even as you are staring at your project folder with ALL the necessary files in place.
  • Code file naming conventions.  With .h and .m files typically used in Xcode for Objective C, C++ conventions and compiler approaches to code type detection may vary.  Remember C++ files should be .mm or .cpp — and in any event it is worth learning how to find read Xcode build file settings.  There are specific directives about how the compiler should determine source code language, including what rules to use and how to apply them.  Check it out.
  • Near the top of my list of errors that were particularly strange  — like “include file is missing” when it clearly is not — are error messages that say “Could not validate build due to missing Icon.png file.  Icon.png must be 57×57 pixels and found as 0 x 0 pixels”.  The file was there of course and easily verified to be the right size.  In the end this, too, was a build file setting issue — in Xcode 4.2+ i “fixed” a project by setting “pre-compress png files” to “NO”. Not that i’d ever heard of that setting or tried to set it myself before.
The good news is that there is, at the moment, an excellent way to get started and cure all these problems — at least for projects you create.  Check out Kobold2D.  (I link to the site and to Steffen Itterheim’s blog on my sidebar.)  Creating your project templates with Kobold 2D creates a complete app shell with all the third party packages you might want to include and bulletproof build files that remove ALL the sorts of problems identified above.  The limitation of this approach is that it means those great ‘sample projects’ you are working on are usually best re-implemented in a clean project template; there possibility of “fixing up” a project that has already packaged in the wrong version of something, or has build settings mangled beyond recognition, is slim to none.  I started down the path of upgrading an old Cocos2D project per the Kobold2d advice, and quickly saw it would be more work than starting the project over and moving in the bits of code that were of interest.
I am hoping Kobold2D is good for the long run, but right now it adds enough by itself that it is worth making part of your base development environment — particularly for newcomers to the platforms or tools.

Leave a Reply

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

You are commenting using your 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