与MinGW的静态和动态/共享链接

[英]Static and Dynamic/Shared Linking with MinGW


I want to start with a simple linking usage to explain my problem. Lets assume that there is a library z which could be compiled to shared library libz.dll(D:/libs/z/shared/libz.dll) or to static library libz.a (D:/libs/z/static/libz.a).

我想从一个简单的链接用法开始解释我的问题。让我们假设有一个库z可以编译为共享库libz.dll(D:/libs/z/shared/libz.dll)或静态库libz.a(D:/ libs / z / static / libz) 。一个)。

Let I want to link against it, then I do this:

让我想链接它,然后我这样做:

gcc -o main.exe main.o -LD:/libs/z/static -lz

According to this documentation, gcc would search for libz.a, which is

根据这个文档,gcc会搜索libz.a,这是

archive files whose members are object files

归档文件,其成员是目标文件

I also can do the following:

我也可以做以下事情:

gcc -o main.exe main.o -LD:/libs/z/shared -lz

It is not mentioned in the documentation above that -l flag will search for lib<name>.so.

上面的文档中没有提到-l标志将搜索lib .so。

What will happen if I libz.a and libz.dll will be in the same directory? How the library will be linked with a program? Why I need the flags -Wl,-Bstatic and -Wl,-Bdynamic if -l searches both for shared and static libraries?

如果我libz.a和libz.dll将在同一目录中会发生什么?图书馆如何与计划挂钩?为什么我需要标志-Wl,-Bstatic和-Wl,-Bdynamic如果-l搜索共享库和静态库?

Why some developers provide .a files with .dll files for the same modules, if I compile a shared library distribution?

如果我编译共享库分发,为什么有些开发人员为.a文件提供相同模块的.dll文件?

For example, Qt provides .dll files in bin directory with .a files in lib directory. Is it the same library, but built like shared and static, respectively? Or .a files are some kind of dummy libraries which provide linking with shared libraries, where there are real library implementations?

例如,Qt在bin目录中提供.dll文件,其中包含lib目录中的.a文件。它是相同的库,但分别构建为共享和静态?或者.a文件是某种虚拟库,它提供了与共享库的链接,其中有真正的库实现?

Another example is OpenGL library on Windows. Why every compiler must provide the static OpenGL lib like libopengl32.a in MingW?

另一个例子是Windows上的OpenGL库。为什么每个编译器都必须在MingW中提供类似libopengl32.a的静态OpenGL库?

What are files with .dll.a and .la extensions used for?

什么是.dll.a和.la扩展名用于的文件?

P.S. There are a lot of questions here, but I think each one depends on the previous one and there is no need to split them into several questions.

附:这里有很多问题,但我认为每个问题都取决于前一个问题,没有必要将它们分成几个问题。

1 个解决方案

#1


26  

Please, have a look at ld and WIN32 (cygwin/mingw). Especially, the direct linking to a dll section for more information on the behavior of -l flag on Windows ports of LD. Extract:

请看看ld和WIN32(cygwin / mingw)。特别是,直接链接到dll部分,以获取有关LD的Windows端口上-l标志行为的更多信息。提取:

For instance, when ld is called with the argument -lxxx it will attempt to find, in the first directory of its search path,

例如,当使用参数-lxxx调用ld时,它将尝试在其搜索路径的第一个目录中查找

libxxx.dll.a
xxx.dll.a
libxxx.a
cygxxx.dll (*)
libxxx.dll
xxx.dll

before moving on to the next directory in the search path.

然后转到搜索路径中的下一个目录。

(*) Actually, this is not cygxxx.dll but in fact is <prefix>xxx.dll, where <prefix> is set by the ld option -dll-search-prefix=<prefix>. In the case of cygwin, the standard gcc spec file includes -dll-search-prefix=cyg, so in effect we actually search for cygxxx.dll.

(*)实际上,这不是cygxxx.dll,但实际上是 xxx.dll,其中 由ld选项-dll-search-prefix = 设置。在cygwin的情况下,标准的gcc规范文件包括-dll-search-prefix = cyg,所以实际上我们实际上搜索cygxxx.dll。

NOTE: If you have ever built Boost with MinGW, you probably recall that the naming of Boost libraries exactly obeys the pattern described in the link above.

注意:如果您曾使用MinGW构建Boost,您可能还记得Boost库的命名完全符合上面链接中描述的模式。

In the past there were issues in MinGW with direct linking to *.dll, so it was advised to create a static library lib*.a with exported symbols from *.dll and link against it instead. The link to this MinGW wiki page is now dead, so I assume that it should be fine to link directly against *.dll now. Furthermore, I did it myself several times with the latest MinGW-w64 distribution, and had no issues, yet.

在过去,MinGW中存在直接链接到* .dll的问题,因此建议使用* .dll中的导出符号创建一个静态库lib * .a,并将其链接到它。这个MinGW wiki页面的链接现在已经死了,所以我认为现在可以直接链接到* .dll。此外,我使用最新的MinGW-w64发行版自己做了几次,但没有任何问题。

You need link flags -Wl,-Bstatic and -Wl,-Bdynamic because sometimes you want to force static linking, for example, when the dynamic library with the same name is also present in a search path:

您需要链接标志-Wl,-Bstatic和-Wl,-Bdynamic,因为有时您希望强制静态链接,例如,当搜索路径中也存在具有相同名称的动态库时:

gcc object1.o object2.o -lMyLib2 -Wl,-Bstatic -lMyLib1 -Wl,-Bdynamic -o output

The above snippet guarantees that the default linking priority of -l flag is overridden for MyLib1, i.e. even if MyLib1.dll is present in the search path, LD will choose libMyLib1.a to link against. Notice that for MyLib2 LD will again prefer the dynamic version.

上面的代码段保证为MyLib1覆盖-l标志的默认链接优先级,即使搜索路径中存在MyLib1.dll,LD也会选择libMyLib1.a进行链接。请注意,对于MyLib2,LD将再次选择动态版本。

NOTE: If MyLib2 depends on MyLib1, then MyLib1 is dynamically linked too, regardless of -Wl,-Bstatic (i.e. it is ignored in this case). To prevent this you would have to link MyLib2 statically too.

注意:如果MyLib2依赖于MyLib1,那么MyLib1也是动态链接的,无论-Wl,-Bstatic如何(即在这种情况下它被忽略)。为了防止这种情况,您还必须静态链接MyLib2。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2013/04/06/ab960f41b5484af18034f16a07a59cd0.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号