A user (@spcman) has the following setup:
- libpng16 installed from a Linux package repository
- libpng12 built and installed locally by user
The former installed its headers and libraries in /usr/include and /usr/lib respectively, the latter to /usr/local/include and /usr/local/lib.
The problem arises with the default search paths: by default the compiler (gcc) looks for headers by searching /usr/local/includes then /usr/include, whilst the linker (ld) searches for libraries in /usr/lib then /usr/local/lib. Thus, the libpng12 headers were being used with the libpng16 libraries.
Fortunately, libpng versions are designed to co-exist, so in this case we can use pkg-config to resolve the issue. We use it to fetch the specific path to the default libpng headers (which happens to be those of libpng16), then insert said path into the search order before the default system locations, causing the compiler to look there first.