Things I learned about Xcode today

Posted by Orville Bennett on July 16, 2008
Read time: about 1 minute

There are two ways to include files in your project that guarantees compilation. Only one of these ways should be used at a time.

The first method is the one I learned many moons ago: manually. This uses an #inlcude directive and the path to the file enclosed in quotation marks, i.e. #include "someplace/somefile". The second, Xcode specific method, is checking the “Target” checkbox. This checkbox is in the “Target” column, identifiable by the tiny “target” icon in the column header.
Just in case I lost you.

Whenever you add new file to your project in Xcode 3.1, the “target,” i.e. the executable being built, will be automatically selected. Little did I know. I never pay attention to these “details.” After all, I’m the super, bees knees programmer guy. I already know it all. Right?

If, as I did initially, you have your file (SimpleCat.cpp for me) added to a target, and you explicitly #include it you’ll make gcc cry. Actually it will complain about duplicate symbols when linking. Incidentally the same thing happens on the command line so this isn’t something Xcode is doing all by itself.

If, for example, you have a file SimpleCat.cpp included in main.cpp and you try to use the command

g++ SimpleCat.h SimpleCat.cpp main.h main.cpp -o simplekitteh

You will be presented with the exact linking error shown by xcode. If you remove the SimpleCat.cpp file from the command, or comment out its include in main.cpp (and leave the command as is) simplekitteh will purr along nicely.

That is all.
Took me about an hour or two to figure this out.