Difference between static and dynamic library in X

2019-04-18 06:19发布

问题:

What is the difference between a static and dynamic library in XCode? And why doesn't Apple allow us to use dynamic libraries in our iOS applications?

回答1:

While you can build dynamic libraries for Mac OS X, you cannot use them for iPhone development.

A static library is merely an archive of object files that gets pulled into a program linking against it. The linker will unarchive all the archive files, and pull them in during linking along with the rest of your object files.

A dynamic library however, creates a shared object file, akin to a program but without an entry point, which programs can link against and call out of themselves into these shared libraries for their symbols, without pulling them into itself.



回答2:

A dynamic library wouldn't make any sense for an iphone app as there is no way to install the library on the phone. I remember reading some documentation where apple stated they decided not to use dynamic libraries as they didn't want users to have to deal with hassles of finding/updating libraries. Much easier to just install 1 bundle per app.



回答3:

Apple does allow you to make dynamic libraries. On Mac OS X, these end in .bundle or .dylib (not .so or .a like on Linux).

What, specifically are you trying to do? Did you create a target for your dylib?



回答4:

Static libraries (*.a) are collections of object files. In its turn, object file is just a name for a file that comes out of a compiler and contains machine code.

Dynamic libraries (*.dylib) as opposed to the static ones, rather than being copied into single monolithic executable, are loaded into memory when they are actually needed. This could happen either at load time or at runtime.

Dynamic libraries are usually shared between applications, therefore the system needs to store only one copy of the library and let different processes access it. As a result, invoking code and data from dynamic libraries happens slower than from the static ones.

All iOS and macOS system libraries are dynamic. Hence our apps will benefit from the future improvements that Apple makes to standard library frameworks without creating and shipping new builds. Apple reserves the ability to create system frameworks for itself; there is currently no way for third-party developers to create system frameworks on iOS.

If your Deployment target is iOS8+ you are able to create embedded framework that is embedded framework and dynamic framework. Embedded frameworks are placed within an app’s sandbox and are only available to that app. This type was created first of all for extensions to share common code and resources.

The source and one more. Also read more here