What are "targets" in Xcode? - xcode

I have seen a lot of things about 'targets' while working with xcode. when i click on the project icon (blue, in xcode 4.2) i see 'Targets' in the middle pane with my file name under it.
What does that mean? and can I add more 'targets' to my project? If yes, what would that mean?
I have this:
http://developer.apple.com/library/ios/#featuredarticles/XcodeConcepts/Concept-Targets.html
but i didn't understand a thing

A target basically defines what it is you are building and how you are building it. You can add more targets if you would like to build more than one thing. This usually makes sense if you need to build several related things from the same project.
For instance, you might want one target for a full, paid version of an application, and another target for a reduced, free version of an application. Both targets would include much of the same code and resources, but some of the settings would be different and you might have different files included with each.

Targets are simply different ways of building a product. You have your source files, and you have your output, and there are several ways that you can get from A to B.
An Xcode project may contain a number of source files which can be built do produce a number of different products.
For example, you could build an executable using some of the files, or you could build a library (Framework) from some of the files, or a unit test library, or with different build flags.
Most projects will have a single target, but some are more complex than others.

Related

Xcode add multiple frameworks to multiple targets and projects

I have dozens of projects that I need to update, and they have two seperate Targets each. I need to add 9 new Frameworks to each of the Targets in each project.
I know I can use CMD+CLICK to select all 9 frameworks at one time and add them to each Target. However is there any way I can add them to both targets at once?
What about adding all these frameworks to multiple projects at once?
Thanks
I found a solution to this (or kinda of a quicker way than the original way).
First add the framework(s) to the first target.
Go to the Navigator area(left pane of xcode), and select the frameworks folder to see the list of Frameworks.
Click through each framework, and note that on the right pane of Xcode, in File Inspector, it is applied only to the first target, but you see that the other targets are unchecked.
-Check all the additonal targets you want to have the frame work and its now added to that target.
-Repeat steps for all the frameworks you want shared across multiple targets.
No, there does not appear to be anyway to add frameworks to multiple targets or projects at the same time.

Building different targets in different folders - Xcode

I have two targets in the same xcode project, as bundle plugins, and I want the executable files within the binary to have the same name for both targets.
Is there a way I can either:
A) Define the executable file for each binary without affecting the .bundle name (or first target overwrites the second target as they're building).
B) Build both files in their own folder.
They are in the same project, so the build end-results are automatically placed in the same folder. One overwrites the other, as the .bundle name always ends up the same (because I want the same executable name). They share a lot of code, so they are in the same project, to build everything again at once, to make sure everything always has the latest code across all versions.
Would anybody know a way of doing this? I tried various options in the build settings. Or would anybody maybe have any "build phase" workaround ideas? Please don't ignore that the executable name needs to be the same for all binaries.
Thanks in advance!
I created a project for each slightly different built, with flags in the build settings for each target making each target distinct (with use of macros in the actual code).
Regarding in ensuring code is always the up-to-date, partially shared, code for each build/project, they share the source code by adding it to the project, without selecting the "copy to project folder" option. Annoying workaround, but it'll do until I work something better out...
Why don't you use targets with different names? Or a script after build, that copies the target to a different name?
This should copy each target to a unique name after build, without each build overwriting the other. (If things are linear)

Easy way to merge two Xcode project files that contain different targets (Carbon/Cocoa/PPC/Intel)

We have two Xcode project files for a Photoshop Plug-in. One for PS CS2 through CS5 32-Bit and the other for CS5 64-Bit and CS6. I'll not mention PS Elements etc. For CS and PS 7.0 we maintain a separate Codewarrior project - Well, mainly for sports today.
Anyway the PPC plug-in for CS2 and the Universal Binary plug-in (PPC/Intel 32-Bit) are built from one Xcode Project and the 64-Bit Cocoa UI plug-in is built from another Xcode Project. Simply because Adobe never provided an example with 64-Bit and PPC in the same project file. They always just break the old version support in the SDK to fix things for the current PS.
We made it so that all source files are completely interoperable. Just a bunch of #defines control what SDK PC/MAC_CW/MAC_XC/MAC_XC64 we're in and things work. So my question is if there is any easy way to merge the two Xcode Project files so that one contains all targets? It'd be limited to being used with 10.6 of course because of the PPC versions but still I was wondering how to do this.
Before I delve into the merging, you might also want to consider: programmatically building from the command line using xcodebuild for automating the build of all of the individual projects as opposed to merging them, or using Workspaces to contain both projects. I'm not sure if Workspaces is an option in 10.6, though, so that might not work.
Based on your description above (most notably that you are using all of the same files in both projects), I would suggest choosing one of the existing projects and then just duplicating the existing target and modifying it to work for your alternate build requirements.
We have tried to do programmatic and scripted merges of Xcode files in the past and it is often not worth the hassle because the Xcode project files are pretty brittle and contain a lot of complex file references which aren't easy to de-duplicate.
Process would be:
Open the project file you want to use as your final project
Select top level project in the Project Navigator (which should make the Project appear in the main area
Select your current target
Choose Edit > Duplicate
With the new target, adjust the settings as necessary, taking special care about: Product Name, Deployment Target, etc.
Remove the libraries which aren't necessary for this platform from the list of link objects in the Build Phases tab
Add the additional libraries to the project using File > Add Files...
Add the libraries to the Target's Build Phases (link)
It'll be a bit tedious, but it'll give you better results than trying to do this programmatically.

Xcode -- groups and targets

I have an Xcode project. The project currently has four targets, and I am about to add more. Within the project are about 150 png files. I have all of these in a tree of groups in the Xcode "groups and files" panel.
I want to include these png files in some of my targets, but not in others. The thing is, I will always want either all of them or none of them. I don't want to pick and choose.
Is there a way to manage this? Basically, I want some targets to use all of the files in the "png files" group, and others to use none of them. If there is a way to include an entire tree of groups in a target, and remove the same tree from another target, without having to do each file individually, that would be ideal.
EDIT: starting a bounty on this due to the lack of response. If it's not possible to do the above, I would be interested in other ways to accomplish more or less the same thing -- for example would it help to make all the png files a shared library? Or to make them a target in and of themselves, and have some of my other targets depend on that one? In either case, I would need to know how to set the thing up.
My bottom line is that I want to do the whole thing as simply as possible. For example, it should be easy to add a file to the 150 and tell my project that it should be included in all targets that use that group of files.
I am using the Xcode 3.2.3 right now. I am able to right click on a group, select get info then jump over to the 'Targets' tab and select my targets I want those files to go into. It works for me, is this what you are looking for?
Write a script and include it in the appropriate targets as Run Script Phases. Since it gets environment variables from Xcode it’s pretty easy for the script to copy files over to the right place. You can also pass more information to the script and maintain a clean, code-only Xcode project.
You’ll have a directory full of images, and every time the script runs (as you build the targets) it copies everything. To add an image, you add it to the directory without modifying the Xcode project.

Xcode dependencies across different build directories?

I am trying to set up Xcode for a project which contains multiple executables and static libraries. I have created multiple targets and set up the linking and dependencies, and initially everything works great. The catch...
This is an existing project which already has Visual Studio and Makefile builds. Those builds put the libraries in a lib/Debug directory and the executables in bin/Debug. So in Xcode I changed the Build Products Path to "lib" and "bin" respectively (so we can use one set of documentation for all of the platforms). This puts the compiled targets in the right place, but completely breaks both the linking (Library not found) and the dependencies.
I can fix the linking by adding $(SRCROOT)/lib/Debug to the Library Search Paths for each executable (but it feels like Xcode should be able to figure this out on its own, which makes me think I'm doing something wrong).
But — I can't figure out how to get the dependencies working again. If I change a library source file, the library will rebuild but not the dependent executables. If I force a build of the executable Xcode returns success without doing anything; it thinks the target is up to date. If I clean the target and then rebuild it works.
So, any ideas here? Is Xcode being fundamentally stupid in this regard, or is it me (I'm leaning toward the latter)?
Update: I've posted a sample project to demonstrate the issue at http://share.industriousone.com/XcodeDepsIssue.zip. Build it once, then modify MyStaticLib.c and build it again. The executable will not relink (and it should). Many thanks for any help on this one.
starkos, thanks for publishing your conclusion. It validated my experience as well. This situation really screwed me, so it was nice to know I wasn't just missing something.
I did however discover a workaround that avoids creating multiple projects or keeping the library and its dependent in the same directory. It is a hack, but it does work here.
I know it's a bit late but better than never.
For the dependency library, add a "Copy Files Build Phase", with Absolute Path as the destination, and the path text field should be the directory where the DEPENDENT target lives. Then click on Products, find the dependency library (will end with .a), and drag it into the "Copy Files Build Phase." If you now build, this will put the library into its own directory like before and THEN also copy it into the dependent's target directory.
For the dependent, you can now remove the dependency's output directory from the Library Search Paths. This will cause it to find the library copy. If you do this, the dependent will indeed be relinked each time the dependency .a is relinked.
The negatives are, of course, the extra time for the copy, and the necessity to specify (in the Copy phase) the target directory for each dependent of your library. Beats the hell out of the alternatives though....
Xcode doesn't automatically set up dependencies based on use of build products; you have to set up explicit target dependencies yourself.
Project > Edit Target Settings, General tab, + button, add any targets that are prerequisites to building the selected target. That should get you going again.
I've researched this some more and the answer is no, Xcode 3.x doesn't track dependencies between targets that live in different directories. You can work around it by giving each library its own project, and adding each of those to a master project. Or you can keep all of your targets in one directory. Pick your poison.
Here is my solution for this weird behavior in xcode 4.3.1. You have to add build pre-action in scheme:
rm -f ${BUILT_PRODUCTS_DIR}/${EXECUTABLE_PATH}
and choose which build settings to use for this script. Each time before build, target executable will be removed and rebuild completely. It helped for me, and i hope it helps you.
NOTE: Have tried to put this script in project build phase, and result was negative - debugger could not connect process to start debugging.
Good luck!
OK, it would help to have the text of the Linking... build line that's failing. But a couple of things:
1) You shouldn't be linking to anything in $(SRCROOT). That's your project sources. The two places to find things to link are $(SYMROOT) (the Build Products directory) or $(DSTROOT) (the Installed Products directory).
One thing you could do is to have a common Build Directory, then use 'xcodebuild install' action to install the products in the Installation Directory. The other is to use a Copy Files build phase to copy them after building, so you can link against them in $(SYMROOT) but still have them where your Windows compatriots expect them.
THere is probably a way to set up the per-target build products directories correctly, but I'd really have to see the project itself to figure it out.

Resources