How to add_subdirectory after a externalproject_ad

2019-07-28 17:48发布

问题:

I have to download zlib to use in my project. I use externlproject_add() to downaload all zlib repository, build and install it. After it, I want do install a lib that is part of zlib repository: minizip.

How to set this dependency on cmake?

zlib module:

cmake_minimum_required ( VERSION 2.8.7 )
include (ExternalProject)

if(UNIX)  
# An external project for zlib 

SET (GIT_URL https://github.com/madler/zlib.git) 
SET (ZLIB_INSTALL ${CMAKE_CURRENT_BINARY_DIR}) 
SET (ZLIB_INCLUDE ${CMAKE_BINARY_DIR}/include/zlib) 
SET (ZLIB_STATIC  ${CMAKE_BINARY_DIR}/lib/libz.a )
SET (MINIZIP_DIR ${CMAKE_CURRENT_BINARY_DIR}/ZLIB/src/ZLIB/contrib/minizip)

ExternalProject_Add(zlib     
    PREFIX zlib     
    GIT_REPOSITORY ${GIT_URL}     
    INSTALL_DIR ${ZLIB_INSTALL}     
    PATCH_COMMAND ${CMAKE_COMMAND} -E remove <SOURCE_DIR>/zconf.h     
    BUILD_IN_SOURCE 1     
    PATCH_COMMAND ""     
    CONFIGURE_COMMAND <SOURCE_DIR>/configure --prefix=<INSTALL_DIR> --includedir=${ZLIB_INCLUDE}     
 )

 find_package(ZLIB REQUIRED)
 if(ZLIB_FOUND)
      add_subdirectory (${MINIZIP_DIR})
 endif(ZLIB_FOUND)

SET (ZLIB_INCLUDE_DIR ${ZLIB_INSTALL}/include/zlib) 
SET (ZLIB_LIBRARY "${ZLIB_INSTALL}")
ADD_LIBRARY (ZLIB_LIB STATIC IMPORTED DEPENDS zlib)
SET_TARGET_PROPERTIES (ZLIB_LIB PROPERTIES IMPORTED_LOCATION "${ZLIB_STATIC}")

endif(UNIX)

With this zlib module I have an error that following directory

/home/lais/Imagens/agent/build/ZLIB/src/ZLIB/contrib/minizip

doesn't exist yet when I run

cmake ..

And it's true. Doesn't exist yet. I should tell cmake it. But I don't know how to do it.

回答1:

I solved it download a zip file that have minizip.

cmake_minimum_required ( VERSION 2.8.7 )
include (ExternalProject)

project (zlib)

if(UNIX)
  # An external project for zlib 
  SET (URL http://www.winimage.com/zLibDll/unzip101e.zip) 
  SET (MINIZIP_INSTALL ${CMAKE_CURRENT_BINARY_DIR}) 
  SET (MINIZIP_STATIC  ${CMAKE_BINARY_DIR}/lib/minizip.a )
  message ("zlib = ${zlib}")
  ExternalProject_Add(minizip
    DEPENDS zlib     
    PREFIX minizip     
    URL ${URL}     
    INSTALL_DIR ${MINIZIP_INSTALL}   
    BUILD_IN_SOURCE make    
    CONFIGURE_COMMAND ""
  )

  SET (MINIZIP_LIBRARY "${MINIZIP_INSTALL}")
  ADD_LIBRARY (MINIZIP_LIB STATIC IMPORTED DEPENDS minizip, zlib)
  SET_TARGET_PROPERTIES (MINIZIP_LIB PROPERTIES IMPORTED_LOCATION "${MINIZIP_STATIC}")
endif(UNIX)


标签: cmake zlib