与 libpng 和 libpng 链接兹库?
我正在尝试编译一个同时使用 libjpeg 和 libpng 的项目。我知道 libpng 需要 zlib,因此我独立编译了这三个文件,并将它们(libjpeg.a、libpng.a 和 libz.a)放在名为 linrel32
的文件夹中。然后我执行的是:
g++ -Llinrel32/ program.cpp otherfile.cpp -o linrel32/executable -Izlib/ -Ilpng140/ -Ijpeg/ -lpthread -lX11 -O2 -DLINUX -s -lz -lpng -ljpeg< /code>
所以我包含了这三个库。尽管如此,链接器仍然抱怨:
linrel32//libpng.a(png.o): In function `png_calculate_crc':
png.c:(.text+0x97d): undefined reference to `crc32'
linrel32//libpng.a(png.o): In function `png_reset_crc':
png.c:(.text+0x9be): undefined reference to `crc32'
linrel32//libpng.a(png.o): In function `png_reset_zstream':
png.c:(.text+0x537): undefined reference to `inflateReset'
linrel32//libpng.a(pngread.o): In function `png_read_destroy':
pngread.c:(.text+0x6f4): undefined reference to `inflateEnd'
linrel32//libpng.a(pngread.o): In function `png_read_row':
pngread.c:(.text+0x1267): undefined reference to `inflate'
linrel32//libpng.a(pngread.o): In function `png_create_read_struct_2':
(...你明白了:D)
collect2: ld returned 1 exit status
我知道缺少的函数来自 zlib,并且我在那里添加了 zlib。打开libz.a,看起来结构不错。重新编译一下,一切看起来都很好。但不是……
我不知道,很可能问题是微不足道的,我需要的是睡一会儿。但是,如果你能帮我解决这个问题......
I'm trying to compile a project that uses both libjpeg and libpng. I know that libpng needs zlib, so I compiled all the three independently and put them (libjpeg.a, libpng.a and libz.a) on a folder called linrel32
. What I execute then is:
g++ -Llinrel32/ program.cpp otherfile.cpp -o linrel32/executable -Izlib/ -Ilpng140/ -Ijpeg/ -lpthread -lX11 -O2 -DLINUX -s -lz -lpng -ljpeg
So I include the three libraries. Still, the linker complains:
linrel32//libpng.a(png.o): In function `png_calculate_crc':
png.c:(.text+0x97d): undefined reference to `crc32'
linrel32//libpng.a(png.o): In function `png_reset_crc':
png.c:(.text+0x9be): undefined reference to `crc32'
linrel32//libpng.a(png.o): In function `png_reset_zstream':
png.c:(.text+0x537): undefined reference to `inflateReset'
linrel32//libpng.a(pngread.o): In function `png_read_destroy':
pngread.c:(.text+0x6f4): undefined reference to `inflateEnd'
linrel32//libpng.a(pngread.o): In function `png_read_row':
pngread.c:(.text+0x1267): undefined reference to `inflate'
linrel32//libpng.a(pngread.o): In function `png_create_read_struct_2':
(... you get the idea :D)
collect2: ld returned 1 exit status
I know the missing functions are from zlib, and I'm adding zlib there. Opened libz.a and it seems to have a good structure. Recompiled it, everything looks fine. But it is not...
I don't know, is likely that the problem is trivial, and what I need is to sleep for a while. But still, if you could help me to figure out this thing ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要重新排列库的顺序:
发生的情况是链接器对于如何处理静态库有特殊的规则。它的作用是,如果需要 .o 来满足引用,则它仅包含 .a 内部的 .o。
此外,它按照静态档案在链接行上出现的顺序处理静态档案。
因此,您的代码不会直接调用 zlib 中的任何函数。因此,当链接器首先处理 -lz 时,还没有任何对它的调用,因此它不会拉入任何 zlib。
接下来,当链接器处理 libpng 时,它会看到代码中对它的调用。因此它从 libpng 中提取代码,并且由于它调用 zlib,所以现在有对 zlib 函数的引用。
现在您到达了库的末尾,并且存在未满足的调用,这会导致您的错误。
因此,如果 libhigh.a 使用 liblow.a,则链接顺序中必须有
-lhigh
before-llow
。You need to rearrange the order of the libraries:
What is happening is that the linker has special rules on how it treats static libraries. What it does is that it only includes a .o from inside the .a if the .o is needed to satisfy a reference.
Furthermore, it handles static archives in the order in which they appear on the link line.
So, your code does not directly call any functions in zlib. So when the linker handles -lz first, there are not yet any calls to it so it doesn't pull in any of zlib.
Next, when the linker handles libpng, it sees that there are calls to it from your code. So it pulls the code from libpng and since it makes calls to zlib, now there are references to the zlib functions.
Now you come to the end of your libraries and there are unsatisfied calls which causes your error.
So, if libhigh.a makes use of liblow.a, you must have
-lhigh
before-llow
in your link order.您可能需要用
extern "C"
包围 zlib 和 png 标头,例如:you probably need to surround the zlib and png headers with
extern "C"
, e.g.: