Search code examples
ioscompilationlinkerstatic-librariesdynamic-library

Are both static libraries and dynamic libraries compiled during the build process?


When you build an app, You have to compile and then link. Whether you link a framework dynamically or statically, you still have to compile them both.

It’s just that for a static library, you link at build time and pay its price through extra time. With a dynamic library, you link at launch time i.e. defer the extra cost, but eventually you’ll have to endure the link time…

Like my question is, for dynamic linking, the compilation still happens during build time. It’s not like that the OS has a compiler and compiles and then links app launch time. I imagine if that was correct then app launches for dynamic linking would be terrible…

Do I have it right?

And if a library is pre-compiled, then while you save on the compilation step, it doesn't in any way affect the linking. Still — depending on if it's static vs. dynamic library you will have to pay the cost of linking eventually.


Solution

  • Not an IOS-specific question.

    Libraries (static or dynamic) are not necessarily compiled at build time. Many are provided as binary files without source code and simply linked to your program. If you have the source code, you can set up your build process to build the libraries.

    Statically-linked libraries are combined into your executable file at build time by the linker. Dynamically-linked libraries are not part of your executable file, and are linked to your program at run time by the operating system's program loader.

    Dynamic linking is generally very fast. Modern compile-time linkers can be very sophisticated and can do exotic things like generate and compile code.

    So far we're describing classic compilation and linking.

    Now, there are newer languages that are a hybrid of compilation and interpretation. In these languages, a library contains an intermediate representation (IL) that is may be interpreted by a runtime, or compiled to native code just-in-time (JIT).

    We haven't even discussed interpreted languages like Javascript.

    Modern systems often blur the traditional distinctions between interpreted and compiled languages and do "all of the above". Sometimes the code running to accomplish something in an app is not even on your device, but is running on a server "in the cloud".

    So, your homework now is to read up on the terms "native code", "assembly code", "intermediate language", "compiler", "JIT", "runtime", and "interpreted language".