C:与MinGW-w64静态/动态链接的正确方法 [英] C: Correct Way to Statically / Dynamically Link with MinGW-w64

查看:866
本文介绍了C:与MinGW-w64静态/动态链接的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

直观地:

  • MinGW-w64是GNU编译器工具(GCC等)的Windows端口.
  • 用于Windows的预编译二进制文件是.dll(动态链接)/.lib(静态链接).
  • 但是,MinGW-w64使用GNU编译器工具,因此需要.so/.a二进制文件.
  • MinGW-w64 is a Windows port of the GNU compiler tools (GCC, etc.).
  • Pre-compiled binaries for Windows are .dll (dynamic linking) / .lib (static linking).
  • However, MinGW-w64 uses the GNU compiler tools, it would follow that .so / .a binaries are required.

我发现的东西:

  • According to Red Hat Enterprise Linux documentation, seems that MinGW/MinGW-w64/Cygwin linkers look for .dlls and .as
  • According to this tutorial, you should dynamically link to .so and statically link to .a.
  • One question on SO indicates you can statically link a .lib and another on SO says it doesn't work.

不幸的是,我找不到MinGW&的明确文档. MinGW-w64指出动态/静态链接库时正确和错误的地方.

Unfortunately, I can't find explicit documentation from MinGW & MinGW-w64 that says what's right and what's wrong when dynamically/statically linking libraries.

根据我的经验,我一直能够动态链接到.dll.一次,我能够静态链接到.lib(使用-static标志).

In my experience, I have always been able to dynamically link to .dlls. Once, I was able to statically link to a .lib (using the -static flag).

问题:

使用MinGW-w64 GCC工具链进行编译时,.dll/.a二进制文件是否适合动态和静态链接库?换句话说,是为MSVC生成的动态库,还是为GCC生成的静态库?

Are .dll / .a binaries appropriate to dynamically and statically link libraries when compiling with the MinGW-w64 GCC tool-chain? In other words, dynamic libraries generated for MSVC and static libraries generated for GCC?

推荐答案

答案:

MinGW/MinGW-w64的ld可以:

MinGW / MinGW-w64's port of GCC's linker ld can:

  • 直接链接到.dll进行动态链接
  • 间接链接到.dll.a进行动态链接(在编译时使用导入库)
  • 链接到.a进行静态链接.
  • directly link to .dlls for dynamic linking
  • indirectly link to .dll.as for dynamic linking (using import library at compilation)
  • link to .as for static linking.

为什么MinGW/MinGW-w64的GCC链接器端口查找.dll?

Why does MinGW / MinGW-w64's port of the GCC linker look for .dll?

简而言之,最佳答案是因为.dll是Microsoft在其32位和64位操作系统上共享对象的答案.在Windows上,MinGW/MinGW-w64的端口使用Microsoft C运行时(msvcrt.dll)

In short, the best answer is because that's .dlls are Microsoft's answer for shared objects on their 32-bit and 64-bit operating systems. On Windows, MinGW / MinGW-w64's port uses Microsoft C runtime (msvcrt.dll) [1], so it obeys Windows OS linker rules.

动态链接库(或DLL)是Microsoft在Microsoft Windows和OS/2操作系统中对共享库概念的实现. -来自维基百科

因此,要动态链接库,您将使用文件扩展名:

So, to dynamically link libraries you'll use the file extension:

  1. .so for Linux上的共享库,因为这是GCC binutils的链接程序搜索的内容,
  2. Windows上共享库的
  3. .dll,因为这是GCC binutils链接程序的MinGW/MinGW-w64端口所搜索的.
  1. .so for shared libraries on Linux because that's what the GCC binutils' linker searches for,
  2. or .dll for shared libraries on Windows because that's what the MinGW / MinGW-w64 port of GCC binutils' linker searches for.

GCC的MinGW端口用于共享库对象的扩展名在源代码的cygming文件中明确列出.正如@ChronoKitsune所评论的,特别是:SHLIB_EXT = .dll"rel =" noreferrer > libgcc/config/i386/t-slibgcc-cygming . cygming文件(用于Cygwin和MinGW)对于MinGW,MinGW-w64以及Cygwin的32位和64位版本都是通用的.因此,对于Windows的GCC binutils的所有端口都是如此.

The extension used by the MinGW port of GCC for shared library objects is explicitly listed in a cygming file in the source code. As @ChronoKitsune commented, specifically: SHLIB_EXT = .dll in libgcc/config/i386/t-slibgcc-cygming. The cygming files (for Cygwin and MinGW) are common to MinGW, MinGW-w64, and both 32-bit and 64-bit versions of Cygwin. Therefore, this is true for all ports of the GCC binutils to Windows.

为什么MinGW/MinGW-w64链接器随后会处理.lib?

Why does the MinGW / MinGW-w64 linker handle .lib then?

原则上,GCC binutils的链接器不会将.lib识别为静态库.但是,链接器可能足够聪明,可以链接.lib导入的.dll(在.lib实际上是导入库的情况下).例如,在库具有动态链接的依赖项的情况下,该库将动态链接(和强制"静态链接的标志将被忽略).

In principle, the GCC binutils' linker won't recognize a .lib as a static library. However, it is possible that the linker is smart enough to link against the .dll that a .lib imports (in the case that the .lib is actually an import library). For instance, in the case that a library has dependencies which are linked dynamically that library will be linked dynamically (and flags to "force" static linking will be ignored).

在这种情况下,我想链接器不会抛出任何错误,并且看起来好像.lib实际上已成功链接.

In such cases, I'd imagine that the linker would not throw any errors and it would appear as though the .lib was actually linked successfully.

导入库如何工作? (免费赠品)

在Windows上,.lib可以是两个库之一:

On Windows, a .lib can be one of two libraries:

  1. 由编译器从.dll生成的 import 库,其中包含在编译过程中用于符号解析的所有必需定义(但是,省略了函数实现)
  1. An import library generated by the compiler from a .dll with all needed definitions for symbol resolution during compilation (however, function implementations are left out) [2]
  1. 如果您尝试使用MinGW/MinGW-w64的GCC binutils端口为xxxx.dll生成导入库,则会生成libxxxx.dll.a.扩展名文件扩展名对于将 import 库与完全定义的 static 库区分开来很有用.使用MSVC进行编译时,此该扩展名在扩展名中并不明显
  1. If you attempt to generate import libraries for a xxxx.dll with MinGW / MinGW-w64's port of GCC binutils, it will produce a libxxxx.dll.a. The extension file extension is useful for distinguishing an import library from a fully-defined static library. When compiling with MSVC, this distinction isn't apparent in the extension

  • 完全定义的静态库
  • .lib具有双重作用,因为正如@ChronoKitsune所述,MSVC链接器不会直接链接到.dll.而是需要一个导入库来解析编译时的符号定义,以便在运行时不加载.dll:

    .libs serve a dual purpose because, as @ChronoKitsune commented, the MSVC linker does not directly link against .dlls. Instead, an import library is necessary to resolve symbol definitions at compilation, so that the .dll is not loaded until run-time:

    要链接的导入库(.LIB文件). (构建DLL时,链接器创建导入库.)- VS 2015文档

    为什么MinGW/MinGW-w64的GCC链接器端口寻找.a?

    Why does MinGW/ MinGW-w64's port of the GCC linker look for .a?

    这很简单-端口使用* -nix系统上使用的ar归档实用程序,如@ChronoKitsune所评论的:

    This is simple - the port make use of the ar archiving utility that is used on *-nix systems, as @ChronoKitsune commented:

    静态库的扩展来自binutils随附的ar(存档)程序.您可以使用ar -t libxxx.a列出任何静态库中包含的目标文件.

    The extension for static libraries comes from the ar (archive) program included with binutils. You can use ar -t libxxx.a to list the object files contained within any static library.

    这类似于MSVC的lib命令,lib /list foo.lib如果.lib是静态库,则此命令将在其中返回.obj文件的列表.

    This is similar to the lib command for MSVC, lib /list foo.lib This command will return a list of .obj files inside if the .lib is a static library.

    这篇关于C:与MinGW-w64静态/动态链接的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆