conditional link library when switch build from simulator to device - iphone

I have an iphone application using the third party library from my customer. They provide me two static library one for simulator and one for device. How can I change setting to allow me set to project setting to build against right library when project change configuration? can i use --framework flag? if it is, how I can do that?

Easiest way might be to merge the two libraries with lipo. The linker will choose the appropriate architecture's code during the build. This blog post discusses the process (in the context of a much more complete pseudo-framework setup).
See also this question and this question.

Related

Xcode llvm link static library based on defined macro

I am building an iOS app where I want to be able to start my app in a demo mode with mocked connections. I use a static library for the mocking. However I don't want this library to be linked in my release build for the app store. I am looking for a solution to this problem.
The best solution I could imagine would be if I can define a compiler macro like this:
#define TEST_ENVIRONMENT 1
and if this macro is 1, the library would be linked. But I don't know how to setup the linking for this. Is there anything similar that would work?
I know I could create a new configuration in Xcode and manually add the -l option for the library for this configuration but this means that it can easily happen that one edits a build setting and forgets to edit the test configuration as well, or vise versa.
Best regards,
Michael

What exactly is an Target in Xcode?

I was always wondering what's up with those Targets? What is it all about? What's the point of that? I never had to fiddle around with them, but obviously I can. Why should I want that, and what can I do with them? What's their purpose?
Each project can build multiple executables or libraries or call out to a makefile or shell script to build "stuff". Each one of these is a Target.
One iPhone project I have includes a separate target for each static library in my home grown SDK and a shell script target to build the Doxygen docs. Another project includes two targets, one for the app as used by general users, one for an administration & management edition.
In the first example, I need to build each library then link all the static libraries into an SDK test application, so my SDK Test App depends on all the library targets (but not the docs, since I don't need to constantly regen them.)
In the second example, the management and the general versions of the app share a considerable amount of code and resources. When I change one, I want to change them both.
The target is something like a "blueprint". It includes rules that tell the compiler what to do, which sources should be compiled, which files should be copied into the application bundle, which libraries should be linked.
If you want to make a Free-Version of your app one way to do so is to add a new target.
Of course you could just duplicate the whole project but then you had to keep those in sync if you change some code. Using a different targets makes this a lot easier.

How to build command line tool in iphone project

I just started programming in Xcode and I'm trying to write an iphone application. I started out with a simple template for Iphone development and took it from there. Now I would like to build a command line tool (for converting data) which reuses some of the classes in my iphone project. I managed to add a new target 'convert', link the appropriate source code to the target, make the target dependent on the main target. Everything builds, so far so good, but it does not generate the right executable, it will build only build/Debug-iphonesimulator/convert which is not runnable from the command line (I'm guessing because it is not linked with the right libraries).
~/Documents/XCode/SQLiteTutorial> ./build/Debug-iphonesimulator/convert
dyld: Symbol not found: _OBJC_CLASS_$_NSCharacterSet
Referenced from: /Users/marc/Documents/XCode/SQLiteTutorial/./build/Debug-iphonesimulator/convert
Expected in: /System/Library/Frameworks/Foundation.framework/Versions/C/Foundation
in /Users/marc/Documents/XCode/SQLiteTutorial/./build/Debug-iphonesimulator/convert
Trace/BPT trap
Is this possible with xcode at all? I'm not sure what kind of extra information you would need to answer this question, so let me know what to provide, if anything.
Kind regards,
Marc van Kempen.
What if you simply created a different project entirely and then dragged in the relevant classes you wanted to reuse, but instead of copying them, merely used references to them? Just don't select 'copy items into destination's folder' option when you drag the class files into your groups & files pane. That way your CL project always uses the latest version from the main iPhone one.
Having it all in one project would no doubt be cleaner though.
iPhone SDK does not officially support building command-line tools. iPhone does not run command-line tools; at least, not without jailbreaking.

Best practices for MacOS/iPhone library cross compiling

I've build a static library working nice in a Cocoa Touch environment. Now I'd like to compile it also for Cocoa.. Can I have a single XCode project with different sdk targets? Is there some resource out there able to give hints about best the practices in this (and other) sense?
This last two months I have been working on exactly this task ( cross compiling static library for iPhone/Android/Mac OS/Linux/Windows...
It is certainly possible, a nice way, is adding an external xcode project as a target to your first xcode project. So you create a new "Active Configuration" for Mac OS X, iPhone and other platforms that you want to support.
Here, you can find a good tutorial about how to use a secondary Xcode project as a target of your main project to build a static library. It's a cool way because if you debug for example you still have all the symbols of the library, etc.
It can be done but it requires some manual tweaking of the build.
Start with the Xcode Build System Guide.
As an informal way of accomplishing this, you can create two separate projects and add references for exact same set of library source files to each project. Set one project to compile for Cocoa-Touch and the other for Cocoa. If both projects reference the same files, changes made in one project will be automatically reflected in the other. (If you have both projects open, Xcode will complain that the file has been changed by another app but otherwise it won't notice.)
I have a utility class that I continually dump new methods in. I add it to every project and just park methods as I need it. The new methods show up in old projects because the source files are shared across all the projects.

iPhone xcode search path for device vs simulator?

I am confused at this setting (Project -> Edit Active Target).
The Search Paths can differ based on the SDK setting (simulator vs device).
But if I provide both simulator and device paths, for lets say the Frameworks path, then i get linker errors. But it seems if I only provide the proper path for whichever SDK i have selected, then it builds fine!
How can I keep both path settings? Currently Im having to cut and paste the appropriate path based on the SDK i have selected to build.
Thanks!
Which kind of search path are you talking about? The system search paths are automatically handled for you, so I assume that your problem is some custom library.
There are two solutions. You can use conditional settings, or you can use universal libraries. I've grown to love universal libraries, but haven't had time to write up full instructions yet. The way they work is to build a static library for the simulator and for the device, and then use lipo to glue them together. You can then use the same library for both platforms. I really need to write up full instructions for this because it's very useful.
There are two more approaches. First you can use conditional settings. In xcconfig files (see my talk on why to use xcconfig files), you put something like this:
LD_FLAGS[sdk=iphonesimulator*] = -lsasl2
That links sasl2 just for the simulator. Set whatever you flag you need. Another solution is variable substitution:
HEADER_SEARCH_PATHS = "$(SRCROOT)/MyPackage/build/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)/include"
This assumes that MyPackage is in a subdirectory of your project and it was built into the build directory. It'll look in, for example, Debug-iphoneos for its variables.
You can also do both of the above in the build pane, but I really recommend folks get away from the build pane for any serious project. Variable substitution works identically in the build pane, and conditional settings are accessible from right-clicking on a setting.
You should have two separate build target profiles set up, one for sim and one for device, rather than constantly editing the same one. That's kind of the point of targets.
If you're only using
project headers
SDK framework headers
sqlite3 headers
then your Header Search Paths should be empty. Xcode provides search paths for your project headers, SDK frameworks, and /usr/include/*.h automatically, and adjusts those for the framework in use.
The only reasons to have custom Header Search Paths is when you have references to headers that are not in the SDK, are in "deep" locations in the SDK (such as in subdirectories of /usr/include or in buried frameworks), or are in other targets or projects your project cross-references.
Rob already hinted at this, but to clarify, here's how you'd set it using the build window.
In the build settings window, select the setting you want to modify (like "Framework search paths"). Then click the gear in the lower left, and select "Add Build Setting Condition." Then you can add a value that applies only to iOS Simulator builds, and a second build setting condition to apply only to device builds.