Linker error with libpng under MacOSX

MacOS

Question or issue on macOS:

I’m working on MacOSX 10.7.2 and Xcode 4.2.1. I installed libpng using port and I was trying to load a PNG image in my application, but I get linker errors:

Undefined symbols for architecture x86_64:
  "_png_create_read_struct", referenced from:
      loadPngImage(char*, int&, int&, bool&, unsigned char**) in test.o
  "_png_create_info_struct", referenced from:
      loadPngImage(char*, int&, int&, bool&, unsigned char**) in test.o
  "_png_destroy_read_struct", referenced from:
      loadPngImage(char*, int&, int&, bool&, unsigned char**) in test.o
  "_png_set_longjmp_fn", referenced from:
      loadPngImage(char*, int&, int&, bool&, unsigned char**) in test.o
  "_png_init_io", referenced from:
      loadPngImage(char*, int&, int&, bool&, unsigned char**) in test.o
  "_png_set_sig_bytes", referenced from:
      loadPngImage(char*, int&, int&, bool&, unsigned char**) in test.o
  "_png_read_png", referenced from:
      loadPngImage(char*, int&, int&, bool&, unsigned char**) in test.o
  "_png_get_IHDR", referenced from:
      loadPngImage(char*, int&, int&, bool&, unsigned char**) in test.o
  "_png_get_rowbytes", referenced from:
      loadPngImage(char*, int&, int&, bool&, unsigned char**) in test.o
  "_png_get_rows", referenced from:
      loadPngImage(char*, int&, int&, bool&, unsigned char**) in test.o
ld: symbol(s) not found for architecture x86_64

I included png.h in my project using

#include "/usr/X11/include/png.h"

I know libpng is based on zlib, thus I have included -lz in “Other linker flags” but nothing changed.

Any suggestions on how to make it work?

How to solve this problem?

I solved with a manual installation of libpng:

  • download the source from official web site
  • in Terminal, go in the downloaded folder and launch

    cp ./scripts/makefile.darwin makefile make sudo make install make clean 
  • if it doesn’t work (as in my case) open makefile with TextEdit (or equivalent) and change line

    ARCH="-arch i386 -arch x86_64"

    in

    ARCH=-arch x86_64

    (assuming, of course, your system is 64 bit).

It may not be enough. Xcode was still unable to find the library. I solved using

cd /usr/local/lib
sudo ln -s libpng15.dylib ./libpng15.15.dylib

That did the trick. Now it works fine.

Hope this helps!