cmake层次结构zlib,libpng和我自己的应用程序

时间:2011-07-29 14:44:11

标签: c++ cmake zlib libpng

我正在尝试为使用libpng的应用程序创建CMake层次结构。 Libpng需要zlib。

由于CMakeLists.txt与zlib和libpng一起分发,我的第一个想法是制作以下结构:

/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?-

...然后,在顶级CMakeLists.txt中,添加如下内容:

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

但没有运气。 libpng的CMakeLists.txt执行find_package(ZLIB ...),但它不知道在哪里查看。这可以通过将zlib“安装”到/ usr在Mac OS上解决。但这在Windows中无效。

那么我想我不会进入子目录。只需编译并构建zlib和libpng,然后在遍历我自己的应用程序之前执行find_package(PNG ...) (单独编译和构建zlib和libpng(通过提供的CMakeLists.txt)至少在Mac OS上工作,但仅仅因为zlib安装到/ usr)。

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

没有运气,find_package(PNG ...)失败了。我不知道如何让find_package(PNG ...)知道在哪里寻找我刚建立的libpng库。例如,对于boost,您可以设置“BOOST_ROOT”变量。 libpng有什么类似的东西吗?

亲切的问候,

Daniel Dekkers

2 个答案:

答案 0 :(得分:1)

似乎查找png没有PNG_ROOT变量,就像BOOST_ROOT的情况一样。我怀疑这是zlib库的情况。您可以通过查找find_png模块和find_zlib模块来检入您的模块目录。

我会重写这些模块并将它们添加到您的cmake configure目录中。重写的版本应如下所示:

# 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)

请注意,我正在添加两次find_命令。第一次跳过默认目录。第二个不跳过默认目录。如果第一次搜索成功,则第二次搜索未完成。如果定义了PNG_INCLUDE_DIR或PNG_LIBRARY,第二次搜索将知道第一个被取代。

答案 1 :(得分:1)

您必须打开PNG_BUILD_ZLIB选项,以防止libpng使用find_package查找zlib。

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