Targeting Leopard, using blocks when available

A scenario cropped up recently where I want to pass a block into an API when running on Snow Leopard and greater, but fall back to the old block-less API before that. Sounds simple enough, but launch the app on Leopard and what’s this?

Symbol not found: __NSConcreteStackBlock
Expected in: /usr/lib/libSystem.B.dylib

Uh-oh. What to do? I tracked down this Stack Overflow answer for the same problem with iOS3, and applied it to Sandvox. It works! In Xcode simply add this to your target’s “Other Linker Flags”:

-weak_library /usr/lib/libSystem.B.dylib


© Mike Abdullah 2007-2012