Random Mac OS X Coding Tips
- Darwin’s in-bult
malloc(3) memory allocator has a lot of extra features to help you track down memory leaks. It’s no Valgrind nor BoundsChecker (oh how I’d love Valgrind for Mac OS X), but it works quite well in conjunction with the
leaks(1) command-line tool. See the
malloc(3) manpage for details. Note that this has nothing to do with MallocDebug, which is a separate, much more heavyweight
malloc implementation (which is also pretty useless, IMHO).
- Both static and dynamic linking on Mac OS X is a little complex, partially because of NeXT’s Mach-based baggage, and partially because things such as two-level namespaces aren’t needed as much on Linux, where source-code rather than binary compatibility is the norm. Read and learn the
ld(1) manpage for the main system linker, and getting to know the
dyld(1) manpage dynamic linker is pretty useful too.
- You can use the
gcc_select command-line tool to switch the default gcc compiler between gcc 2.95, 3.1, 3.3 and 4.0 (all of which are installed with the Xcode developer tools). Note that only gcc 4.0+ will actually compile stuff successfully for x86 architectures, so if you
gcc_select back to gcc 3.3 and try to compile pretty much anything, chances are good that it won’t work at all.
- The ever-useful
ldd(1) on Linux, which displays dependencies for object files, is known as
otool -L on Mac OS X.
- Apple’s Shark profiler is, by far, the best profiler I have ever seen on any platform (though I haven’t used Intel’s VTune yet, which I hear is also pretty damn good). If you need to profile anything, take the five minutes to walk through the Shark tutorial, and be blown away by the amount of information it will cheerily give you in one of the best interfaces for a developer tool that I’ve seen.
- If you’re a command-line junkie and work with Cocoa and Xcode a lot, remember that you can type in
open *.xcodeproj to open an Xcode project file in the current directory. I use this so often that I have it as a shell alias named
xcode (well, a shell script really, but the different is irrelevant).