I am using libpng in my project. Right now, I can compile my project with: g++ *.cpp `libpng-config --ldflags`
I want to switch to using CMake for easy recompiling
I finally solved it using find_package
. Thanks to this blog post.
find_package(PNG REQUIRED)
include_directories(${PNG_INCLUDE_DIR})
target_link_libraries(${MY_EXEC} ${PNG_LIBRARY})
I think the recommended and portable way it should be done using pkg-config, something like this:
# search for pkg-config
include (FindPkgConfig)
if (NOT PKG_CONFIG_FOUND)
message (FATAL_ERROR "pkg-config not found")
endif ()
# check for libpng
pkg_check_modules (LIBPNG libpng16 REQUIRED)
if (NOT LIBPNG_FOUND)
message(FATAL_ERROR "You don't seem to have libpng16 development libraries installed")
else ()
include_directories (${LIBPNG_INCLUDE_DIRS})
link_directories (${LIBPNG_LIBRARY_DIRS})
link_libraries (${LIBPNG_LIBRARIES})
endif ()
add_executable (app_png ${_MYSOURCES} ${LIBPNG_LINK_FLAGS})