I'm not yet a fan of integrated development environments, but I'm trying to overcome my prejudices and learn Xcode. (Eclipse/CDT is next; I couldn't get that to work for me either when I tried last year, but that's a separate issue.)
I am writing some new code in a new project that will become (part of) a small library. I want to unit test it, too. How do I explain to Xcode that I'm building a (shared) library, but I also want to use it in a test program, compiled from separate source that won't be in the shared library?
Source code:
- atom.c
- atom.h
- test-atom.c
Produced files:
- libatom.dylib
- test-atom
I've got atom.c
and atom.h
compiled into the library. I'm just not sure how to organize things so that I can also build test-atom
to link with the library.
I'm assuming that when I've got that sorted, adding the library for the test support code that test-atom.c would be relatively straight-forward – even though it isn't under Xcode control yet.
FWIW, I primarily work in C rather than Objective C.
Best Answer
You need two targets in your project; a target in Xcode produces a product which is a library, executable, or some other output.
Thus you'd have a target to produce
libatom.dylib
, which I suspect you've already set up, and another command-line executable target to produce thetest-atom
executable for you to run to test your library.Once you've added the
test-atom
target, you should Get Info ontest-atom.c
and remove its membership from thelibatom.dylib
target, and add it as a member of your newtest-atom
target. The target membership of a file is what determines whether building a target will try to compile/copy/link that file. (What the target does with the file depends on what build phase it gets added to when it's made a member.)You should also Get Info on the
libatom.dylib
entry in your Products group, and make that a member of thetest-atom
target as well. That will cause thetest-atom
executable to link againstlibatom.dylib
.Finally, Get Info on the
test-atom
target (not the product) and in the General tab, add a dependency on thelibatom.dylib
target. This will ensure that building thetest-atom
target will always first build thelibatom.dylib
target.