Conditional link a library on iOS
ios, xcode
Solution
I actually haven't tried to do this with build flags directly, but I've done it with the Xcode GUI settings. Select your build Target, then Build Phases, and then choose to add your static library to the list of binaries to link.
However, select Optional (which is not the default) from the Required/Optional menu on the right.
Since this is a static library you're talking about, I think you'd then need to put some preprocessor guards in your code, to disable use of the library in the simulator:
#if TARGET_IPHONE_SIMULATOR
NSLog(@"do nothing here!");
#else
HelloLibrary* hl = [[HelloLibrary alloc] init];
NSString* result = [hl helloLibraryFoo];
#endif
I did nothing else to make this work (no other Build Settings were modified).
When building for the simulator, I just get this warning:
ld: warning: ignoring file /Users/me/Desktop/code/MyAppName/libHelloLibrary.a, file was built for archive which is not the architecture being linked (i386): /Users/me/Desktop/code/MyAppName/libHelloLibrary.a
Problem
I want to link a library conditionally (I have this library for iOS device, but I don't have it for Simulator). I am using Xcode 4.6 and iOS 6.1. I read a question (and couple similar ones): iOS conditional link static library -weak_library linker flag I tried ry to build the project with following flags: ``` -weak_library LibraryNameWithPath ``` However it gives me an error: ``` ld: file not found: LibraryNameWithPath ``` -weak-l linker flag I tried to build it with following flags: ``` -weak-lShortLibraryName ``` And got the same results : ``` ld: library not found for -lShortLibraryName ``` Thoughts Why the heck, does it check for library existence, if it is explicitly marked to be a weak link? Is there a way to do conditional linking in build time (vs runtime usage of dlopen, dlclose and friends)?