简体   繁体   English

关于静态/隐式链接与动态/显式链接的误解

[英]Misconception about static/implicit linking Vs dynamic/explcit linking

I've recently learnt that static linking and implicit linking are basically the same thing , just different nomenclature.我最近了解到静态链接和隐式链接基本上是一回事,只是命名不同。 My understanding is that when you statically (implicitely) link to a binary, you are by definition linking against a *.lib (windows) or *.a (linux) file, often using target_link_libraries in cmake.我的理解是,当您静态(隐式)链接到二进制文件时,您根据定义链接到*.lib (windows)或*.a (linux)文件,通常在 cmake 中使用target_link_libraries On the other hand when you explicitely link (using LoadLibrary on windows) you are by definition linking to a *.dll file (or *.so on linux) (and there is no corresponding cmake command because all the work is done inside the actual code).另一方面,当您明确链接(在 Windows 上使用LoadLibrary )时,您是根据定义链接到*.dll文件(或*.so在 linux 上)(并且没有相应的 cmake 命令,因为所有工作都是在实际的内部完成的代码)。

However, in multiple places I've read people referring to statically/implicitely linking to a dll file, which has confused me.但是,在多个地方,我读到有人提到静态/隐式链接到dll文件,这让我感到困惑。 Clearly there is a hole in my knowledge somewhere and I was hoping somebody here could plug it.显然,我的知识在某处存在漏洞,我希望这里有人可以填补它。

Edit编辑

Its been pointed out that this question refers mainly to windows, which it does.有人指出,这个问题主要是指窗户,它确实如此。 However, I am currently trying to produce cross platform code so I am still interested on how (or if) these concepts generalise to other platforms.但是,我目前正在尝试生成跨平台代码,因此我仍然对这些概念如何(或是否)推广到其他平台感兴趣。

There are actually 3 different kinds of linking, not 2.实际上有 3 种不同的链接,而不是 2 种。

For UNIX:对于 UNIX:

  1. Link against archive (aka static) library:链接存档(又名静态)库:

    gcc main.o libfoo.a

  2. link against dynamic (aka shared) library:链接到动态(又名共享)库:

    gcc main.o libfoo.so

  3. Link against libdl , which allows you to dlopen arbitrary other shared libraries (which don't need to exist at the time of the link):链接libdl ,它允许您dlopen任意其他共享库(链接时不需要存在):

    gcc main.o -ldl

Both 2 and 3 involve dynamic linker (and are using shared libraries), but to a different extent. 2 和 3 都涉及动态链接器(并且使用共享库),但程度不同。

An equivalent exists on Windows: when you link against foo.lib , you are using either 1 or 2, depending on whether foo.lib contains actual code, or refers to foo.dll . Windows 上存在一个等效项:当您针对foo.lib链接时,您使用的1 或 2,具体取决于foo.lib是否包含实际代码,或者是指foo.dll

When you use LoadLibrary , you are in case 3.当您使用LoadLibrary ,您处于第 3 种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM