2

I've encountered an issue whilst developing my own application based on libpng (1.5 version).

Long story short, I need to static link this library but I keep getting 'undefined reference' errors. I tried both extracting libpng15.a and adding its content to my library as well as linking with a gcc command.

My gcc command:

gcc test.c librimg.a libz.a libpng15.a -o test -std=c99 -lglfw -lGL -lGLU

Errors I get:

libpng15.a(libpng15_la-png.o): In function `png_reset_crc':
/home/robin/Downloads/libpng-1.5.12/png.c:111: undefined reference to `crc32'
libpng15.a(libpng15_la-png.o): In function `png_calculate_crc':
/home/robin/Downloads/libpng-1.5.12/png.c:152: undefined reference to `crc32'
libpng15.a(libpng15_la-png.o): In function `png_reset_zstream':
/home/robin/Downloads/libpng-1.5.12/png.c:757: undefined reference to `inflateReset'
libpng15.a(libpng15_la-pngread.o): In function `png_create_read_struct_2':
/home/robin/Downloads/libpng-1.5.12/pngread.c:119: undefined reference to `inflateInit_'
libpng15.a(libpng15_la-pngread.o): In function `png_read_row':
/home/robin/Downloads/libpng-1.5.12/pngread.c:557: undefined reference to `inflate'
libpng15.a(libpng15_la-pngread.o): In function `png_read_destroy':
/home/robin/Downloads/libpng-1.5.12/pngread.c:1070: undefined reference to `inflateEnd'
libpng15.a(libpng15_la-pngrutil.o): In function `png_inflate':
/home/robin/Downloads/libpng-1.5.12/pngrutil.c:333: undefined reference to `inflate'
/home/robin/Downloads/libpng-1.5.12/pngrutil.c:362: undefined reference to `inflateReset'
libpng15.a(libpng15_la-pngrutil.o): In function `png_read_finish_row':
/home/robin/Downloads/libpng-1.5.12/pngrutil.c:3848: undefined reference to `inflate'
/home/robin/Downloads/libpng-1.5.12/pngrutil.c:3880: undefined reference to `inflateReset'
collect2: ld returned 1 exit status

(Note: I've compiled library from source and directory /home/robin/Downloads/libpng-1.5.12/ is a place where I extracted the archive.)

It's probably worth mentioning that I can link to a shared library without any difficulties:

gcc test.c librimg.a -o test -std=c99 -lpng15 -lglfw -lGL -lGLU

I would appreciate any help.

Robin92
  • 533
  • 1
  • 6
  • 20
  • This could be because of the way the linker works. Try putting `libz` *after* `libpng` - the linker generally scans each library in turn, resolving symbols that are currently known to be unresolved. In your example, it processes `libz` before it knows that it needs anything from `libz`, because it hasn't seen `libpng` yet... – twalberg Mar 20 '14 at 18:07

2 Answers2

1

I managed to resolve this issue by myself.

I copied all libpng15.a and libz.a to my project's directory and extracted them. Then I packed all objects files (including mine own) into one library (*.a). This is working :)

Robin92
  • 533
  • 1
  • 6
  • 20
0

I had problem with creating qrencode executable (FUKUCHI Kentaro libqrencode) with staticaly linked libpng (version 1.5). I have resolved it thanks to your post by realizing I nned to link libz. So the solution is adding dynamic libz.

Here is my command line:

./configure --prefix=${TARGET_FOLDER} CFLAGS=-mmacosx-version-min=10.6 png_LIBS=/usr/local/Cellar/libpng/1.5.17/lib/libpng15.a LDFLAGS=/usr/lib/libz.dylib
Marcel Vyberal
  • 101
  • 1
  • 6