Avoid Automatic Framework Linking in Swift

Avoid automatic framework linking in swift

Apparently this feature is called autolinking. Swift compiler implicitly emits additional linker flags that links in all the modules the source code depends on (import Dynamic).

There is no way to disable this entirely. But there is a private compiler flag that allows you to disable autolinking for a single framework: swiftc -disable-autolink-framework <framework>.

Some references:
https://gist.github.com/zrzka/c89705ff634ea01aebc1
https://github.com/niw/automatic_linking_tool/blob/master/README.md

I’m pretty sure you can append -v to swiftc and it will print all underlying invocations as commands. Hopefully you’ll be able to see the linker invocations as well.

You should wrap the the private flag inside two -Xfrontend flags to allow it to be used:

OTHER_SWIFT_FLAGS = "-Xfrontend -disable-autolink-framework -Xfrontend Dynamic"

For this to take effect you must still set Link Frameworks Automatically No under Apple Clang - Language - Modules.

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

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.

Problems linking iOS app with custom framework

First, create the iOS framework project, being sure to set the Installation Directory (INSTALL_PATH) build setting for the framework target to @rpath. I would change the Xcode scheme to make it a shared scheme. Now close the framework project, to be sure it can be opened when we import it into the App project in the following steps.

Next, create the iOS App project. Similar to how you did, I created an App (Debug) scheme and a App (Release) scheme. I created a Frameworks group in the App project similar to the image shown below. Then I located the .xcodeproj file of my framework I just created, and dragged it into that Frameworks group. Open the disclosure triangles to reveal the framework project's Products like shown in the image below. Drag it to the Embedded Binaries section in the General section of the app target. Doing so should automatically add the other steps you would need to take to get things to work properly (I believe these include adding the framewokr as a dependency, linking against the framework, and copying the framework into the built app).

Sample Image

The last step is to make sure the framework can be found at compile time. In the app target's build settings, be sure to set Framework Search Paths (FRAMEWORK_SEARCH_PATHS) to $(BUILT_PRODUCTS_DIR). When you select the scheme for the app that builds in a Develop build style, the framework will be built in the same build style in the same directory as the app itself.



Related Topics



Leave a reply



Submit