FindPNG can get confused when multiple versions of libpng are installed
Many linux systems have multiple versions of libpng installed, and CMake's FindPNG can sometimes get confused and provide mismatched headers and library versions. This is particularly likely if the "default" version of libpng (the one libpng.so links to) is not the latest version.
For example, on my OpenSUSE Tumbleweed system, find_package(PNG) will give the path to libpng16 headers (/usr/include/libpng16
), but the libpng12 library (/usr/lib64/libpng.so
, which is a symlink to /usr/lib64/libpng12.so
).
This can result in somewhat inscrutable errors when available functions do not match, e.g.:
/usr/lib64/gcc/x86_64-suse-linux/12/../../../../x86_64-suse-linux/bin/ld: CMakeFiles/julius.dir/src/graphics/screenshot.c.o: in function `image_write_header':
screenshot.c:(.text+0x26e): undefined reference to `png_set_longjmp_fn'
/usr/lib64/gcc/x86_64-suse-linux/12/../../../../x86_64-suse-linux/bin/ld: CMakeFiles/julius.dir/src/graphics/screenshot.c.o: in function `image_write_rows':
screenshot.c:(.text+0x364): undefined reference to `png_set_longjmp_fn'
collect2: error: ld returned 1 exit status
Using pkg-config to look up individual versions of PNG works, as it will use the correct matching versions.
For example, see this PR for the Julius project, which shows pkg-config being used: https://github.com/bvschaik/julius/pull/674/files