我有以下 cmake 文件要下载、构建和安装 zlib:

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 )

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

  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)

但是这个 cmake 文件只安装 zlib。我也想安装 minizip。 Minizip 是“zlib 的一部分”。在 zlib 存储库中有一个包含 minizip 的目录。

我怎样才能在同一个 cmake 文件中安装 minizip?是否可以?

minizip 位于 zlib 存储库中:
- zlib
    - contrib
        - minizip
        - ....
    - ...
- ...

我有 cmake 文件来安装 minizip:
cmake_minimum_required(VERSION 2.8)

project(minizip)

set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11" )

set(BUILD_SHARED_LIBS OFF)

find_package (ZLIB REQUIRED)

if (ZLIB_FOUND)
  set(ZLIB_LIBRARY ${CMAKE_BINARY_DIR}/ZLIB/src/ZLIB/contrib)
  SET (minizip ${CMAKE_BINARY_DIR}/lib/minizip)

  if(CMAKE_HOST_APPLE)
    set(PLATFORM __APPLE__)
  elseif(CMAKE_HOST_UNIX)
    set(PLATFORM unix)
  elseif(CMAKE_HOST_WIN32)
    set(PLATFORM _WIN32)
  else(CMAKE_HOST_APPLE)
    message(FATAL_ERROR "Not supported Platform")
    endif(CMAKE_HOST_APPLE)

  add_definitions(-D${PLATFORM})

set(SOURCE
  ${ZLIB_LIBRARY}/minizip/ioapi.c
  ${ZLIB_LIBRARY}/minizip/miniunz.c
  ${ZLIB_LIBRARY}/minizip/minizip.c
  ${ZLIB_LIBRARY}/minizip/unzip.c
  ${ZLIB_LIBRARY}/minizip/zip.c
)

if(WIN32)
    set(SOURCE ${SOURCE} ${ZLIB_LIBRARY}/minizip/iowin32.c)
endif(WIN32)

set(HEADERS
  ${ZLIB_LIBRARY}/minizip/crypt.h
  ${ZLIB_LIBRARY}/minizip/ioapi.h
  ${ZLIB_LIBRARY}/minizip/miniunz.h
  ${ZLIB_LIBRARY}/minizip/unzip.h
)

if(WIN32)
    set(HEADERS ${HEADERS} ${ZLIB_LIBRARY}/minizip/iowin32.h)
endif(WIN32)

add_library(minizip ${SOURCE} ${HEADERS})
target_link_libraries(minizip PUBLIC "-static" ZLIB_STATIC)

add_dependencies ( minizip zlib)

install(
  TARGETS minizip EXPORT minizip-exports
  INCLUDES DESTINATION "include"
  RUNTIME DESTINATION "bin"
  LIBRARY DESTINATION "lib"
  ARCHIVE DESTINATION "lib"
)

install(
  FILES ${HEADERS}
  DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/include/minizip"
)

ADD_LIBRARY (MINIZIP_LIB STATIC IMPORTED DEPENDS minizip)
SET_TARGET_PROPERTIES (MINIZIP_LIB PROPERTIES IMPORTED_LOCATION ${minizip})

万一()

我想在安装 minizip 之前,安装 zlib。
但是当我跑



我有以下错误:



我有一个顶级 cmake,它调用这两个模块:
cmake_minimum_required( VERSION 2.8.7 )

project( project )

# version number
set ( VERSION_MAJOR 0 )
set ( VERSION_MINOR 0 )

# cpr requires c++11
set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11" )

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libstdc++ -static-libgcc")

# src : main + jsoncpp library
file ( GLOB SOURCES src/project/*.cpp )

# src : collect functions - depend on OS
if ( WIN32 )
    file ( GLOB SOURCES ${SOURCES} src/project/windows/*.cpp )
else ()     # if( UNIX )
    file ( GLOB SOURCES ${SOURCES} src/project/linux/*.cpp )
endif ()

# src : curl requests

# options for cpr
# avoid experimental use of openssl
option( CMAKE_USE_OPENSSL "Use OpenSSL code. Experimental" OFF )
# avoid building tests
option( BUILD_CPR_TESTS "Set to ON to build cpr tests." OFF )

# options for curl
# avoid building tests
option( BUILD_CURL_TESTS "Set to ON to build cURL tests." OFF )
# avoid running tests - set ON again in download version or if errors occur
option( RUN_CURL_TESTS "Set to ON to run cURL tests." OFF )

add_subdirectory ( lib/cpr )
include_directories ( ${CPR_INCLUDE_DIRS} )
include_directories ( ${CURL_INCLUDE_DIRS} )

# src : DtWinVer - Windows Version/Edition class
if ( WIN32 )
    add_subdirectory ( lib/dtwinver )
endif ()

# headers
include_directories ( "include" )
include_directories ( "${CMAKE_BINARY_DIR}/include" )

# scr = libboost

include ( "modules/boost.cmake" )

# src = zlib
include ( "modules/zlib.cmake" )

# src = minizip
include ( "modules/minizip.cmake" )

# compile
set ( CMAKE_RUNTIME_OUTPUT_DIRECTORY "../bin" )
add_executable ( project-v${VERSION_MAJOR}.${VERSION_MINOR} ${SOURCES} )
target_link_libraries ( project-v${VERSION_MAJOR}.${VERSION_MINOR}     ${CPR_LIBRARIES} ${CURL_LIBRARIES} ${FILESYSTEM_LIB} ${SYSTEM_LIB} ${REGEX_LIB}  ${PROGRAM_OPTIONS_LIB} ${ZLIB_STATIC} ${minizip})

add_dependencies ( project-v${VERSION_MAJOR}.${VERSION_MINOR} Boost)
add_dependencies ( project-v${VERSION_MAJOR}.${VERSION_MINOR} zlib)
add_dependencies ( project-v${VERSION_MAJOR}.${VERSION_MINOR} minizip)


if ( WIN32 )
    target_link_libraries ( project-v${VERSION_MAJOR}.${VERSION_MINOR} dtwinver )
else ()
    # libudev
    target_link_libraries ( project-v${VERSION_MAJOR}.${VERSION_MINOR} udev )
endif ()

最佳答案

看起来您的源包不完整。minizip 只是一个简单的文件 minizip.c 。为什么需要 cmake ?只需编译它并与 zlib 链接,一切都会好起来的。

关于c++ - 如何使用 zlib 安装 minizip?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43784532/

10-12 14:51