Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

framework in ios contain static or dynamic library inside

I have come across multiple sites describing that frameworks can contain both static as well as dynamic library. But how do I identify if what the framework actually contains is a dynamic library or a static library. I thought of first taking help of extension (.a for static library, .dylib for dynamic library) but all I could see inside the framework that I was experimenting was that no extensions were provided for the binary file that was packaged inside. Is there any way I can find whether it's a static library or a dynamic library.

Also I have read Landon Fuller's post on static libraries http://landonf.bikemonkey.org/code/ios/Radar_15800975_iOS_Frameworks.20140112.html in which he mentions that frameworks provide two level namespace for libraries but does it cover normal debug symbols and not just dependencies and does it work even for static libraries packaged inside frameworks.

E.x. what happens if I have the same debug symbol inside a framework as well as in the project codebase to which it is linked. Will two level namespace work in that scenario.

Also according to this author http://ddeville.me/2014/04/dynamic-linking/ framework is a bundle or package containing a dynamic library, header files and resources.

As per the following post Library? Static? Dynamic? Or Framework? Project inside another project it says frameworks can contain both static as well as dynamic libraries.

I am so confused by this. Can somebody explain framework in iOS as to what they contain and how they work in terms of two level namespace.

like image 627
Nav Avatar asked Nov 04 '15 12:11

Nav


People also ask

What is dynamic framework in iOS?

A dynamic framework is a bundle of code loaded into an executable at runtime, instead of at compile time. Examples in iOS include UIKit and the Foundation frameworks. Frameworks such as these contain a dynamic library and optionally assets, such as images.

What is difference between static library and framework in iOS?

Frameworks gives you much more flexibility than static libraries, they can contain resources. But each embedded framework added to project increases startup time as well. If you can ship your source files, Swift packages may be the right choice for you.

How do I know if my framework is static or dynamic?

If you are looking at a binary library / framework (perhaps it's precompiled by a 3rd party) and want to know if it's a static or dynamic binary, just use file command with the path to the binary file. Example (static framework) - static binaries are usually marked with ar archive or similar.

Are CocoaPods static or dynamic?

By default, CocoaPods (no mention of use_frameworks! ) will build and link all the dependencies as static libraries. If we add use_frameworks! to our Podfile, CP will instead build and link the dependencies as dynamic frameworks.


Video Answer


2 Answers

To answer first part of your question, Yes, your understanding is correct that a Framework is just a Directory Structure that bundles the library image (Compiled machine code, headers, resources etc.). To verify if the Framework is actually a Static Library or Dynamic Use the following command

file Path/To/YourLib.framework/YourLib

If the output of a particular architecture (armv7, arm64 etc) says ar archive its a Static Library, on the other hand if for any architecture it says dynamically linked shared library then its unsurprisingly a dynamic library.

If you are creating a Framework Project yourself, You can choose to build a Static or Dynamic Image by setting the Mach-O Type Build Setting of your project.

There are different possibilities in case a same Symbol(For Eg a Function Name) is defined in multiple places. In Almost All of the cases the behaviour would a Link Time Error complaining about multiple Symbols exception being Dynamically Linked Frameworks in which case you might see inconsistent behaviour where a Symbol will be loaded from the Dynamic Framework which was first loaded into the memory.

like image 78
Saumitra R. Bhave Avatar answered Sep 22 '22 16:09

Saumitra R. Bhave


Check static or dynamic

It is simple to determine when a file extension is explicit

  • .a - static library
  • .dylib - dynamic library

Variant 1:

you can use file command

file /some_path/<framework_name>.framework/<framework_name>

//possible results
current ar archive random library //static library
dynamically linked shared library //dynamic library

*It is possible to change it in Build Settings a Mach-O type[About] from Static Library target to Dynamic library as a result .a file will be generated, file command show you dynamically linked shared library but it will be static library

Variant 2: [otool static or dynamic]

*Has the same side effect

[Vocabulary]
[Static vs dynamic framework]

like image 34
yoAlex5 Avatar answered Sep 24 '22 16:09

yoAlex5