[Cmake] finding matching includes and libraries

Wheeler, Frederick W (Research) wheeler at crd.ge.com
Thu May 15 15:42:07 EDT 2003


I have a suggestion on how to find matching includes and libraries in .cmake
files.  This is extracted from FindZLIB.cmake:

FIND_PATH(ZLIB_INCLUDE_DIR zlib.h
  /usr/local/include
  /usr/include
)
FIND_LIBRARY(ZLIB_LIBRARY z
  /usr/local/lib
  /usr/lib                   # (I reordered this line)
)

The potential problem with this is that if there is a broken installation of
zlib in /usr/local/include that has headers but no library, then one might
end up using the headers from /usr/local/include and the library from
/usr/lib.

A potential solution is below.  With this method, we only look for a library
if the header is found and we only look for the library in the same install
prefix as the header.  An added benefit is that there is only one list of
install locations.

FIND_PATH(ZLIB_INCLUDE_DIR zlib.h
  /usr/local/include
  /usr/include
)
IF(ZLIB_INCLUDE_DIR)
  FIND_LIBRARY(ZLIB_LIBRARY z
    ${ZLIB_INCLUDE_DIR}/../lib
  )
ENDIF(ZLIB_INCLUDE_DIR)

I'm not sure about the portability of "${ZLIB_INCLUDE_DIR}/../lib".  Perhaps
there is a better way to strip off the "/include" from ZLIB_INCLUDE_DIR in
CMake.

-Fred Wheeler



More information about the CMake mailing list