C/C++ 动态链接如何在不同平台上工作?

C/C++ How Does Dynamic Linking Work On Different Platforms?(C/C++ 动态链接如何在不同平台上工作?)

本文介绍了C/C++ 动态链接如何在不同平台上工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

动态链接通常如何工作?

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

推荐答案

一一回答你的问题:

  • 动态链接将链接过程的一部分推迟到运行时.它可以以两种方式使用:隐式和显式.隐式地,静态链接器会将信息插入到可执行文件,这将导致库加载并解析必要的符号.明确地,您必须调用 LoadLibrarydlopen 手动,然后 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 or dlopen manually, and then GetProcAddress/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++ 动态链接如何在不同平台上工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:C/C++ 动态链接如何在不同平台上工作?

基础教程推荐