cmake hierarchy zlib, libpng and my own app

Viewed 4137

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

2 Answers

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/)
Related