Don't we need to link framework to XCode project anymore?
Asked Answered
C

3

14

Base on this question

Why don't iOS framework dependencies need to be explicitly linked to a static library

I read the selected answer and still don't understand so I made an example project

Test Project on Github

In the test project, I remove all framework from Link Binary With Libraries and File navigation for both main project and the static library (including Foundation.framework and UIKit.framework too), basically, both project link to 0 frameworks.

Questions are

  • In static library, it's including MapKit/MapKit.h without referencing the Mapkit.framework to the project, why is its still working?
  • In main project, I remove UIKit.framework and Foundation.framework from the project, why is it still working?
  • Since it's working for now, will there be any issue later?

Thank you for your comment.

P.S. By working, I mean I can run on the simulator and I can archive the main project without any error.

Edit 25/07/2014

I tried with the real app that I'm working on, it's the same.

  • I highlight Foundation, UIKit, CoreData and 10 another frameworks in File Navigation, well, all of them.
  • Uncheck the target in Utilities Panel --> Target Membership
  • Build : Pass, Run : Pass

Every functionality of my app is still working as expected. I don't get this.

Concentration answered 23/7, 2014 at 5:48 Comment(0)
H
40

Check your project build settings. Underneath LLVM 5.1 — Language — Modules you should see the option 'Link Frameworks Automatically'. In your case it sounds like it's set to 'YES', the default.

In that case, instead of producing an error when you reference a class that the compiler doesn't know, it'll figure out which Framework contains that class and link it. In your code it'll be MKMapView or one of the other MapKit classes that triggers the linkage.

EDIT: from the relevant 'What's New?' document:

Auto Linking is enabled for frameworks imported by code modules. When a source file includes a header from a framework that supports modules, the compiler generates extra information in the object file to automatically link in that framework. The result is that, in most cases, you will not need to specify a separate list of the frameworks to link with your target when you use a framework API that supports modules.

Another way of looking at it is that the compiler is smart enough to mutate #import to @import when the framework has been built appropriately. All system frameworks have been.

Hailey answered 25/7, 2014 at 9:19 Comment(5)
Thanks @Tommy, yes, it set to YES. If I may ask, default value for Link Frameworks Automatically is YES, right? If that's so, can I say that when I create a new project, I don't need to link to any framework?Concentration
Yes, it's now 'Yes' by default. Also, from opening an old project of mine it looks like it defaults to 'Yes' when you open anything that predates the feature. So I don't think you need to worry about explicitly listing the [properly built] frameworks in the linking step ever again. Pedantically: you'll still be linking to them, it's just that the compiler can figure that out for itself.Hailey
Thanks for the clarification, I guess it's new feature that recently added and I didn't read the LLVM change log so I didn't aware of it.Concentration
@Tommy, thanks for your answer. Is it relevant for swift? If set CLANG_MODULES_AUTOLINK to false, but get the framework linked anyways. I have prepared a supporing project: github.com/Usipov/SwiftAutoFrameworksLinkage Could you take a look at it?Halley
asked a separate question hereHalley
T
2

To elaborate @Tommy's answer, a framework that supports modules satisfies the following 2 conditions:

Under Build Settings > Packaging

  • Defines Module is set to YES
  • Module Map File exists.

So, if you're certain that the framework you're using in your code modularizes like that, you can choose to not explicitly add it in the link phase as it will be automatically added as long as in the project file, under Apple Clang - Language - Modules, The option Link Frameworks Automatically is set to YES.

Treadmill answered 30/4, 2020 at 2:42 Comment(0)
D
0

I recommend cleaning the build and xcode state before attempting to rebuild.

Desalinate answered 14/7 at 9:44 Comment(2)
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewUpstanding
Please, edit and try for How to Answer, describe the effect of what you propose and explain why it helps to solve the problem.June

© 2022 - 2024 — McMap. All rights reserved.