framework in ios contain static or dynamic library

2020-04-09 14:18发布

问题:

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.

回答1:

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.



回答2:

It is simple determine when a file extension is explicit

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

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 in Build Settings a Mach-O type of Static Library target to Dynamic library and generate .a file, file command show you dynamically linked shared library but it will be static library

Vocabulary
Static vs dynamic framework