Monday, January 8, 2007

ZeroLink

I read many posts on Mac programming message boards about problems that end up being caused by ZeroLink. Responding to these posts gets tiresome after a while so I decided to write about ZeroLink. In this post I explain what ZeroLink does, the problems it causes, and how you can turn it off so it doesn't cause you problems.

What ZeroLink Does

Before I explain what ZeroLink does, I should explain how Xcode normally builds C, C++, and Objective C projects. When you tell Xcode to build your project, the compiler compiles each source code file, creating an object file for each source code file. After compiling each file the linker links each object file along with the libraries and frameworks you're using to create an executable file you can run.

As its name suggests, Xcode skips the linking stage when you build with ZeroLink. When you run your program built with ZeroLink, Xcode links object files when your program needs them.

What is the advantage of using ZeroLink? It makes building your project go faster. If your project has many source code files, ZeroLink can speed build times significantly. During development you're going to build your project many times so faster build times save you time.

ZeroLink's Dark Side

ZeroLink's build time savings have a cost. The cost is you can get bizarre program behavior, and the cause of this behavior can be difficult to find. There are two common problems ZeroLink causes. First, because ZeroLink skips the linking stage, you won't find any linker errors when you build your project. The error appears when you run the program in Xcode, and your program crashes.

Second, your program won't launch when you move the executable or try to run it on another Mac. Projects built with ZeroLink can be run only inside Xcode. Because ZeroLink doesn't do any linking, the executable file is just a shell. When you move the executable file and try to launch it, nothing happens because there's no code inside the executable file. The code is in the object files that haven't been linked.

The Cause of Confusion

ZeroLink-related problems occur most often for people new to Xcode, and it's not their fault. When you create a new Xcode project, Xcode sets the project's active build configuration (the configuration it will use when you build the project) to the debug configuration. The debug configuration initially enables ZeroLink. What this means is if you create an Xcode project, add code to the project, and build the project, you're building with ZeroLink, whether you intended to or not.

Most Xcode beginners do not intend to use ZeroLink and have no need for it. Unless you have a large project, the problems ZeroLink causes surpass the time saved by not linking. While ZeroLink has its place, most developers are better off not using it, and Apple would be better off turning off ZeroLink initially.

Turning off ZeroLink

Because Apple enables ZeroLink initially, it is up to you to turn it off. If you're running Xcode 2.2 or later, the easiest way to turn off ZeroLink is to choose Build > Allow ZeroLink. Make sure there is no check mark next to the menu item. No check mark means ZeroLink is turned off. When you turn off ZeroLink using this method, you can't use ZeroLink for any project. If you decide you want to use ZeroLink in the future, choose Build > Allow ZeroLink to give yourself the capability of turning ZeroLink on or off on a project by project basis.

If you're running an earlier version of Xcode, you must turn off the ZeroLink build setting for each project. The ZeroLink build setting is in the Linking build settings collection.