Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linking with libpng & zlib?

Tags:

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

like image 663
huff Avatar asked Mar 07 '10 05:03

huff


1 Answers

You need to rearrange the order of the libraries:

-lpng -ljpeg -lz 

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.

like image 100
R Samuel Klatchko Avatar answered Mar 03 '23 00:03

R Samuel Klatchko