Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional link a library on iOS

Tags:

xcode

ios

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)?

like image 584
Victor Ronin Avatar asked Apr 03 '13 17:04

Victor Ronin


People also ask

How to create a sample iPhone application using the iOS binding library?

Follow these steps to create a sample iPhone application to use the iOS Binding Library created above: Create Xamarin.iOS Project - Add a new Xamarin.iOS project named InfColorPickerSample using the Single View App template:

How to consume a third-party Objective-C library in iOS applications?

When working on iOS, you might encounter cases where you want to consume a third-party Objective-C library. In those situations, you can use a Xamarin.iOS Binding Project to create a C# binding that will allow you to consume the library in your Xamarin.iOS applications. Generally in the iOS ecosystem you can find libraries in 3 flavors:

What are the different types of iOS libraries?

Generally in the iOS ecosystem you can find libraries in 3 flavors: As a precompiled static library file with .a extension together with its header (s) (.h files). For example, Google’s Analytics Library As a precompiled Framework.

How do I create a dynamic link using the iOS builder?

You can use the iOS Builder API to build Dynamic Links from parameters, or to shorten a long Dynamic Link. To create a Dynamic Link, create a new DynamicLinkComponents object and specify the Dynamic Link parameters by setting the object's corresponding properties.


1 Answers

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.

enter image description here

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

like image 196
Nate Avatar answered Sep 23 '22 18:09

Nate