[CMake] should zlib be searched in CMAKE_PREFIX_PATH or am I confused?

Stephen McDowell sjm324 at cornell.edu
Thu Mar 22 12:43:21 EDT 2018


Hi Mario,

Very sorry, I should have looked more closely!  CMAKE_MODULE_PATH is for libraries that install their own CMake scripts.  You are correct, Zlib only installs a pkg-config script.  However, FindZLIB.cmake doesn’t appear to use that at all (aka I don’t believe you need to be setting PKG_CONFIG_PATH).  You should be able to get away with setting ZLIB_ROOT.  So for you I think it would look like

    cmake .. -DZLIB_ROOT=/data/thirdparty

FindZLIB.cmake will proceed to look for ZLIB_ROOT/include/zlib.h and look in ZLIB_ROOT/lib for the library.

The XXX_ROOT is typically available for any built-in CMake FindXXX.cmake modules (see hint at bottom: https://cmake.org/cmake/help/v3.0/module/FindZLIB.html <https://cmake.org/cmake/help/v3.0/module/FindZLIB.html> ).  These FindXXX.cmake modules are for this exact scenario: there is a library that many users want access to that does not install cmake scripts (typically because the library uses a different build system such as autotools).

- - - - -

If you are still having trouble, this is what I used to test this.  I didn’t actually write code that uses it, but I suspect if you remove setting of CMAKE_PREFIX_PATH (I think that’s where your conflict originates from) and just specify ZLIB_ROOT your problem will be solved (I hope!).

With a very crude CMakeLists.txt:

    cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
    project("find_zlib")

    find_package(ZLIB)
    if (ZLIB_FOUND)
      message(FATAL_ERROR "Found Zlib:\n- ZLIB_INCLUDE_DIRS: ${ZLIB_INCLUDE_DIRS}\n- ZLIB_LIBRARIES: ${ZLIB_LIBRARIES}")
    else()
      message(FATAL_ERROR "Did not find Zlib :/")
    endif()

Without setting ZLIB_ROOT:

    $ cmake ..
    -- Found ZLIB: /usr/lib/libz.dylib (found version "1.2.8")
    CMake Error at CMakeLists.txt:6 (message):
      Found Zlib:
      - ZLIB_INCLUDE_DIRS: /usr/include
      - ZLIB_LIBRARIES: /usr/lib/libz.dylib

That’s the default Zlib that comes with OS X.  However, if I specify ZLIB_ROOT:

    $ cmake .. -DZLIB_ROOT=/usr/local/Cellar/zlib/1.2.11/
    -- Found ZLIB: /usr/local/Cellar/zlib/1.2.11/lib/libz.dylib (found version "1.2.11")
    CMake Error at CMakeLists.txt:6 (message):
      Found Zlib:
      - ZLIB_INCLUDE_DIRS: /usr/local/Cellar/zlib/1.2.11/include
      - ZLIB_LIBRARIES: /usr/local/Cellar/zlib/1.2.11/lib/libz.dylib

Let us know if it works or if you still need help!

-Stephen

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20180322/09f3448c/attachment.html>


More information about the CMake mailing list