c -lz library link order (undefined reference to symbol “inflateInit2_”)

若如初见. 提交于 2020-01-30 02:45:34

问题


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

still got error:

undefined reference to symbol "inflateInit2_"

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


回答1:


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.

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

$ gcc foo.c -lbar

, and incorrect to compile with

$ gcc -lbar foo.c

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

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

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.

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)).



来源:https://stackoverflow.com/questions/29199107/c-lz-library-link-order-undefined-reference-to-symbol-inflateinit2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!