简体   繁体   English

静态和共享库符号冲突?

[英]Static and shared library symbol conflicts?

I've got a project working on that's using FreeImage and openCV, currently we're using the jpeg support from both of these (I am working towards fixing that, but for now it's got to stay). 我有一个项目正在使用FreeImage和openCV,目前我们正在使用这两个方面的jpeg支持(我正在努力解决这个问题,但是现在它必须留下来)。 Anyhow, FreeImage compiles libjpeg 7.0 into its static libraries, and openCV's highgui library links it in as a shared library (on my system, Ubuntu 9, I've got libjpeg 6.2 installed). 无论如何,FreeImage将libjpeg 7.0编译成静态库,而openCV的highgui库将它作为共享库链接(在我的系统上,Ubuntu 9,我已经安装了libjpeg 6.2)。

They link into a final library that's used to link into various programs, java wrappers, etc. All of that works fine, no symbol conflicts or anything during compile/link time. 它们链接到一个最终的库,用于链接到各种程序,java包装器等。所有这些工作正常,编译/链接时没有符号冲突或任何东西。 However, when I go to open an image using the openCV cvLoadImage function, it dies when reading the header, most likely due to differences between headers in 6.2 and 7.0. 但是,当我使用openCV cvLoadImage函数打开图像时,它会在读取标题时死亡,这很可能是由于6.2和7.0中标题之间的差异造成的。

If I unlink FreeImage (and comment out the code that requires it), the openCV calls start working again, so clearly the static libjpeg symbols from FreeImage are conflicting with symbols that would be loaded from the libjpeg shared library. 如果我取消链接FreeImage(并注释掉需要它的代码),openCV调用将再次开始工作,因此FreeImage中的静态libjpeg符号与将从libjpeg共享库加载的符号相冲突。 What I can't figure out is why my compiler isn't throwing an error during linking because of the two sets of libjpeg symbols. 我无法弄清楚的是为什么我的编译器在链接期间没有抛出错误,因为有两组libjpeg符号。 Additionally, I've tried replacing my system's jpeglib.h header with the 7.0 version temporarily to see if openCV compiled with that would then sync up with the symbols that freeimage brings to the table, to no avail it seems. 另外,我已经尝试用7.0版本暂时替换我的系统的jpeglib.h头文件,看看openCV编译后是否会与freeimage带来的符号同步,似乎无济于事。

Lastly I put a printf in jpeg_read_header in the libjpeg that freeimage compiles, and rebuilt it to see if openCV is using the freeimage libjpeg definition. 最后,我将一个printf放在libjpeg中的jpeg_read_header中,即freeimage编译,并重建它以查看openCV是否正在使用freeimage libjpeg定义。 It didn't print out so I have to assume not. 它没有打印出来所以我不得不假设。

So I guess my questions are 所以我想我的问题是

1) Why doesn't linking a static libjpeg and a shared libjpeg generate linking errors due to duplicate symbols? 1)为什么不链接静态libjpeg和共享libjpeg会因重复符号而产生链接错误?

2) Does anyone know why these two things are conflicting with one another? 2)有谁知道为什么这两件事彼此冲突?

Edit: Compiling openCV in debug mode and then in regular mode again seems to have knocked something loose and made it work again, no idea what's going on. 编辑:在调试模式下编译openCV,然后在常规模式下再次编译似乎已经松动了一些东西并使其再次工作,不知道发生了什么。

You need to modify the linking options to export only the symbols that you want, then all the symbols in conflict will be hidden internally in the static linking, then no conflicts. 您需要修改链接选项以仅导出所需的符号,然后冲突中的所有符号将在内部隐藏在静态链接中,然后不会发生冲突。 By default all the symbols are exported, that's the problem. 默认情况下,所有符号都会导出,这就是问题所在。 See this link: http://www.gnu.org/software/gnulib/manual/html_node/Exported-Symbols-of-Shared-Libraries.html 请参阅此链接: http//www.gnu.org/software/gnulib/manual/html_node/Exported-Symbols-of-Shared-Libraries.html

it is like this 就是这样

Static libraries are compiled in, dynamic libraries are loaded during runtime, but only those symbols which are missing (I think). 编译静态库,动态库在运行时加载,但只有那些缺失的符号(我认为)。 you can compile shared libraries in, and then you probably get symbol collision. 你可以编译共享库,然后你可能会遇到符号冲突。

so opencv uses symbols which are compiled in, since they already present, rather than those from dynamic libraries. 所以opencv使用编译的符号,因为它们已经存在,而不是动态库中的符号。 you end up using static symbols, possibly with different signatures, from prospective of opencv. 从opencv的角度来看,你最终会使用静态符号,可能有不同的签名。

Generally speaking the linker is fine about being passed multiple libraries that all resolve the same symbol(s). 一般来说,链接器可以通过多个库来解决相同的符号。 It just uses the first one it finds. 它只使用它找到的第一个。 The order of the libraries on your linker command line will determine which one "wins". 链接器命令行上的库的顺序将决定哪一个“获胜”。

This, by the way, is NOT true of object files. 顺便说一下,这不是对象文件。 Every linker I've ever used assumes that you want to use all of the objects that you specify, and will complain if more than one has the same symbol. 我曾经使用的每个链接器都假定您使用您指定的所有对象,并且如果多个具有相同的符号,则会抱怨。

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

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