I'm having a rather strange issue while building a C++ project on OSX using CMake, while pulling in libpng as a dependency. I have libpng 1.6.21 installed via homebrew and the following CMake rules:
FIND_PACKAGE(PNG REQUIRED)
INCLUDE_DIRECTORIES(${PNG_INCLUDE_DIRS})
LINK_DIRECTORIES(${PNG_LIBRARY_DIRS})
ADD_DEFINITIONS(${PNG_DEFINITIONS})
When CMake starts to build and finds the dependencies, it outputs:
-- Found PNG: /usr/local/lib/libpng.dylib (found version "1.4.12")
Investigating further, /usr/local/lib/libpng.dylib
is a symlink to brew's 1.6 version:
$ ls -l /usr/local/lib/libpng.dylib
lrwxr-xr-x 1 fluffy admin 40 Apr 9 16:06 /usr/local/lib/libpng.dylib -> ../Cellar/libpng/1.6.21/lib/libpng.dylib
However, it appears that it is the incorrect png.h
that is being included, as printing out PNG_LIBPNG_VER_STRING
at startup outputs 1.4.12
. And, of course, when I try running my program, I get a version mismatch and the library fails to work:
libpng warning: Application built with libpng-1.4.12 but running with 1.6.21
libc++abi.dylib: terminating with uncaught exception of type std::runtime_error: [write_png_file] png_create_write_struct failed
Using FIND_PACKAGE(PNG)
, the -I
declarations never appear in my build line when I build with VERBOSE=1
. However, if I use the PkgConfig approach:
FIND_PACKAGE(PkgConfig)
PKG_CHECK_MODULES(LIBPNG libpng16 REQUIRED)
INCLUDE_DIRECTORIES(${LIBPNG_INCLUDE_DIRS})
LINK_DIRECTORIES(${LIBPNG_LIBRARY_DIRS})
LINK_LIBRARIES(${LIBPNG_LIBRARIES})
ADD_DEFINITIONS(${LIBPNG_DEFINITIONS})
the correct -I
flag does appear, and yet it's still using the system png.h
instead of Homebrew's.
Is there any way to force the compiler to use homebrew's png.h
? I can't simply uninstall the homebrew libpng since some of my other packages depend on it, including other libraries that this program makes use of.
EDIT: As a temporary workaround I've just added /usr/local/include
to my INCLUDE_DIRS()
and included libpng16/png.h
instead, but this is a fragile hack.
png.h
. Still, getting CMake and CLANG to see the rightpng.h
is proving difficult at best. – Girhiny