tags:

views:

393

answers:

2

Hi, 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 execude 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 figuring out this thing...

Thanks!

A: 

you probably need to surround the zlib and png headers with extern "C", e.g.:

extern "C" {
#include <zlib.h>
}
That is incorrect. If that were the case you would see errors like `undefined reference to: "crc32(unsigned long, char const*, unsigned int)"` and not just `undefined reference to: "crc32"`
R Samuel Klatchko
+1 for correcting me.
+2  A: 

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.

R Samuel Klatchko
Thanks. You're absolutely right. I considered that the order was a possibility, but I didn't have any real reason to support that theory. I mistakenly thought that the linker put everything into a 'pool', and then removed the unused functions after tracing every piece of code.
huff
@huff - you're welcome. FYI, order only matters for static archives; if you used shared objects it would not matter.
R Samuel Klatchko