cmake hierarchy zlib, libpng and my own app
Asked Answered
H

2

9

I'm trying to create a CMake hierarchy for an application that uses libpng. Libpng requires zlib.

Since a CMakeLists.txt is distributed with both zlib and libpng my first idea was to make the following structure:

/development
    CMakeLists.txt
    /zlib-1.2.5
        CMakeLists.txt <- provided by zlib
        -sources-
        -build of zlib?-
    /libpng154
        CMakeLists.txt <- provided by libpng
        -sources-
        -build of libpng?-
    /myapp
        CMakeLists.txt
        -sources-
    /build
        -build of myapp-
        -build of zlib?-
        -build of libpng?-

... and then, in the top level CMakeLists.txt, place something like:

project(everything)
...
add_subdirectory(zlib-1.2.5)
add_subdirectory(libpng154)
add_subdirectory(myapp)
...

But no luck. The CMakeLists.txt of libpng performs a find_package(ZLIB...) but it doesn't know where to look. This might be solved on Mac OS by "installing" zlib to /usr. But this wouldn't work in Windows.

So then i thought i would not recurse into the subdirectories. Just compile and build zlib and libpng independently and do a find_package(PNG...) prior to traversing down into my own app (compiling and building zlib and libpng individually (via the provided CMakeLists.txt) works, at least on Mac OS but again, only because zlib is installed to /usr).

project(everything)
...
find_package(PNG...)
add_subdirectory(myapp)
...

No luck, find_package(PNG...) fails. I have no idea how to let find_package(PNG...) know where to look for the libpng library i have just built. For instance for boost, you can set the "BOOST_ROOT" variable. Is there anything simular for libpng?

Kind Regards,

Daniel Dekkers

Hicks answered 29/7, 2011 at 14:44 Comment(1)
Usually, if the find_package fails, you can edit the CMakeCache file and enter include and library paths there. It's easiest if you use a GUI for CMake.Juxtapose
U
1

It doesn't seem like the find png has PNG_ROOT variable, as is the case with BOOST_ROOT. I suspect that this is the case with the zlib library. You can check in your modules directory by looking for the find_png module and find_zlib module.

I would rewrite these modules and add them to your cmake configure directory. The re-written version should look like:

# This module defines
#  PNG_INCLUDE_DIR, where to find png.h, etc.
#  PNG_FOUND, If false, do not try to use PNG.
# also defined, but not for general use are
#  PNG_LIBRARY, where to find the PNG library.

FIND_PATH(
  PNG_INCLUDE_DIR png.h
  PATHS
    ${PNG_ROOT}
  PATH_SUFFIXES 
    include
    include/libpng
  NO_DEFAULT_PATH )

FIND_PATH(
  PNG_INCLUDE_DIR png.h
  PATHS
    ${PNG_ROOT}
  PATH_SUFFIXES 
    include
    include/libpng )

SET(PNG_NAMES ${PNG_NAMES} png libpng png12 libpng12)

FIND_LIBRARY(
  PNG_LIBRARY 
  NAMES 
    ${PNG_NAMES}
  PATHS
    ${PNG_ROOT}
  PATH_SUFFIXES
    /lib
  NO_DEFAULT_PATHS )

FIND_LIBRARY(
  PNG_LIBRARY 
  NAMES 
    ${PNG_NAMES}
  PATHS
    ${PNG_ROOT}
  PATH_SUFFIXES
    /lib )

# handle the QUIETLY and REQUIRED arguments and set PNG_FOUND to TRUE if 
# all listed variables are TRUE
INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(PNG DEFAULT_MSG PNG_LIBRARY PNG_INCLUDE_DIR)

Note that I'm adding the find_ commands twice. The first time skips the default directories. The second one does not skip the default directories. If the first search succeeds the second one is not done. The second search will know that the first one suceeded if the PNG_INCLUDE_DIR or PNG_LIBRARY is defined.

Uncanonical answered 25/11, 2011 at 18:18 Comment(0)
T
1

You must turn PNG_BUILD_ZLIB option on to prevent libpng from looking for zlib using find_package.

set(PNG_BUILD_ZLIB ON CACHE BOOL "" FORCE)
add_subdirectory(libpng)

# I also had to add some target_include_directories that are
# apparently not set in CMakeLists provided along with zlib and libpng:
target_include_directories(png PUBLIC zlib-1.2.5/ ${CMAKE_CURRENT_BINARY_DIR}/zlib-1.2.5/)
target_include_directories(png PUBLIC libpng154/ ${CMAKE_CURRENT_BINARY_DIR}/libpng154/)
Termination answered 3/3, 2020 at 10:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.