C / C ++动态链接如何在不同平台上工作? [英] C/C++ How Does Dynamic Linking Work On Different Platforms?
问题描述
动态链接通常如何工作?
How does dynamic linking work generally?
在Windows(LoadLibrary)上,您需要在运行时调用.dll,但在链接时,您需要提供相应的.lib文件,否则程序将无法运行。链接... .lib文件包含什么? .dll方法的说明?那不是标题包含的内容吗?
On Windows (LoadLibrary), you need a .dll to call at runtime, but at link time, you need to provide a corresponding .lib file or the program won't link... What does the .lib file contain? A description of the .dll methods? Isn't that what the headers contain?
在* nix上,您不需要lib文件...编译器如何知道标头中描述的方法将在运行时可用?
Relatedly, on *nix, you don't need a lib file... How how does the compiler know that the methods described in the header will be available at runtime?
作为一个新手,当您考虑这两个方案中的任何一个,然后再考虑另一个时,它们都不可行...
As a newbie, when you think about either one of the two schemes, then the other, neither of them make sense...
推荐答案
一个接一个地回答您的问题:
To answer your questions one by one:
-
动态链接将链接过程的一部分推迟到运行时。
可以通过两种方式使用:隐式和显式。
隐式地,静态链接器会将信息插入
可执行文件中,这将导致库加载并解析
必需的符号。明确地,您必须手动调用LoadLibrary
或
dlopen
,然后调用GetProcAddress
/dlsym
您需要使用的每个
符号。隐式加载用于
之类的东西,例如系统库,其实现将取决于
系统的版本,但是接口是有保证的。
显式加载用于插件之类的东西,要加载的
库将在运行时确定。
Dynamic linking defers part of the linking process to runtime. It can be used in two ways: implicitly and explicitly. Implicitly, the static linker will insert information into the executable which will cause the library to load and resolve the necessary symbols. Explicitly, you must call
LoadLibrary
ordlopen
manually, and thenGetProcAddress
/dlsym
for each symbol you need to use. Implicit loading is used for things like the system library, where the implementation will depend on the version of the system, but the interface is guaranteed. Explicit loading is used for things like plug-ins, where the library to be loaded will be determined at runtime.
.lib
文件仅对于隐式加载是必需的。
包含了该库实际上提供了该
符号的信息,因此链接器不会抱怨该符号是
未定义,并且它告诉链接器在哪个库中这些符号
位于,因此它可以插入必要的信息以使
该库自动加载。所有的头文件
告诉编译器,这些符号将存在于某处;
链接程序需要 .lib
知道位置。
The .lib
file is only necessary for implicit loading. It
contains the information that the library actually provides this
symbol, so the linker won't complain that the symbol is
undefined, and it tells the linker in what library the symbols
are located, so it can insert the necessary information to cause
this library to automatically be loaded. All the header files
tell the compiler is that the symbols will exist, somewhere; the
linker needs the .lib
to know where.
在Unix下,所有信息是从
.so
中提取的。我不知道为什么Windows需要两个单独的文件,而不是
将所有信息都放在一个文件中。它实际上是
的大部分信息,因为 .lib
中所需的
信息在 .dll中也需要
。
(也许是许可问题。您可以使用
.dll
分发程序,但是没有人可以链接到库,除非
有一个 .lib
。)
Under Unix, all of the information is extracted from the
.so
. Why Windows requires two separate files, rather than
putting all of the information in one file, I don't know; it's
actually duplicating most of the information, since the
information needed in the .lib
is also needed in the .dll
.
(Perhaps licensing issues. You can distribute your program with
the .dll
, but no one can link against the libraries unless
they have a .lib
.)
保留的是,如果要隐式加载,
必须向链接器提供适当的信息,
带有 .lib
或 .so
文件,以便它可以将
信息插入可执行文件中。而且,如果要显式加载
,则不能直接引用库
中的任何符号;您必须致电 GetProcAddress
/ dlsym
自己获取其
地址(并进行一些有趣的强制转换才能使用)他们)。
The main thing to retain is that if you want implicit loading,
you have to provide the linker with the appropriate information,
either with a .lib
or a .so
file, so that it can insert that
information into the executable. And that if you want explicit
loading, you can't refer to any of the symbols in the library
directly; you have to call GetProcAddress
/dlsym
to get their
addresses yourself (and do some funny casting to use them).
这篇关于C / C ++动态链接如何在不同平台上工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!