c -lz 库链接顺序(对符号“inflateInit2_"的未定义引用) [英] c -lz library link order (undefined reference to symbol "inflateInit2_")

查看:34
本文介绍了c -lz 库链接顺序(对符号“inflateInit2_"的未定义引用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照这个顺序链接了 CodeBlocks 中的库,

I link the the library in CodeBlocks in this order,

-lz
-L/usr/local/lib
-L/usr/local/include
-pthread
-lswscale
-lavutil
-lavcodec
-lmp3lame
-lopus
-ltiff
-lvorbis
-ltheora
-ltheoraenc
-ltheoradec
-lvorbisenc
-ltiffxx
-llzma
-lva
-lavfilter
-lavformat
-lfreetype

仍然有错误:

undefined reference to symbol "inflateInit2_"

我想知道是不是库链接顺序问题?我应该把 -lz 放在哪里?

I am wondering whether it is the library link order problem? Where should I put the -lz?

推荐答案

对于 GCC 和 Clang(可能也包括 Intel 编译器),规则是命令行中较早的引用由稍后指定的库满足命令行.

For GCC and Clang (and probably e.g. the Intel compiler too) the rule is that references from earlier on in the command line are satisfied from libraries specified later on in the command line.

例如,如果 foo.c 引用了库 bar 中的函数,那么用

For example, if foo.c references functions from the library bar, then it's correct to compile with

$ gcc foo.c -lbar

,和不正确的编译

$ gcc -lbar foo.c

因此,最安全的做法是将 -lz 放在最后,这样它就可以满足前面指定的所有库的引用.

Therefore, your safest bet would be to put -lz last, so that it can satisfy reference from all the libraries specified earlier.

这是 gcc(1) 手册页(-l 选项)中的相关引用:

Here's a relevant quote from the gcc(1) man page (-l option):

在命令中编写此选项的位置有所不同;链接器按照指定的顺序搜索和处理库和目标文件.因此,foo.o -lz bar.o 在文件 foo.o 之后但在 bar.o 之前搜索库 z>.如果 bar.o 引用了 z 中的函数,则这些函数可能无法加载.

It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, foo.o -lz bar.o searches library z after file foo.o but before bar.o. If bar.o refers to functions in z, those functions may not be loaded.

然而,更好的可能是使用例如pkg-config(1)--libs 以获得特定库所需的标志.一些库还为此目的提供了自定义脚本(例如,sdl(2)-config 用于 SDL(2)).

However, even better might be to use e.g. pkg-config(1) with --libs to get the flags you need for a particular library. Some libraries also ship with custom scripts for this purpose (e.g., sdl(2)-config for SDL(2)).

这篇关于c -lz 库链接顺序(对符号“inflateInit2_"的未定义引用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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