静态库和动态库是否在构建过程中被编译?

huangapple go评论71阅读模式
英文:

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.

答案1

得分: 1

代码部分不要翻译:

Not an IOS-specific question.
(这不是特定于IOS的问题。)

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).
(现在,有一些新的编程语言是编译和解释的混合体。在这些语言中,库包含中间表示(IL),可以由运行时解释,或者即时编译成本地代码(JIT)。)

We haven't even discussed interpreted languages like Javascript.
(我们甚至还没有讨论解释性语言,如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".
(所以,您现在的作业是学习“本地代码”,“汇编代码”,“中间语言”,“编译器”,“JIT”,“运行时”和“解释性语言”这些术语。)

英文:

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".

huangapple
  • 本文由 发表于 2023年2月19日 05:03:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75496388.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定