我的环境正在使用CLion,运行CMake和Mingw。
我需要在我的项目中使用zlib,但我不能在我的生活中让它在windows中链接。
如果我按照this stackoverflow article的解决方案,尝试使用上述文章中的确切cmakelists构建zlib示例二进制文件,则会收到错误:
Could NOT find ZLIB (missing: ZLIB_INCLUDE_DIR)
我也无法在官方zlib网站上找到zlib的预编译二进制文件,zlib自己的文档说明应该存在于网站上,所以试图将其链接到我的项目上很困难。
我确实找到了一些二进制文件,但在尝试链接它们时,(我还在试图弄清楚如何将dll与cmake链接起来,这可能是问题的一部分),make系统只是nopes进行。
mingw32-make.exe[1]: *** [CMakeFiles/CMinecraft.dir/rule] Error 2
mingw32-make.exe: *** [CMinecraft] Error 2
CMakeFiles\Makefile2:153: recipe for target 'CMakeFiles/CMinecraft.dir/rule' failed
答案 0 :(得分:0)
Personnaly for windows(VS 2015/2017,但应该与Mingw合作)我将它作为CMake子项目集成使用:
在你的CMakeLists.txt中:
cmake_minimum_required(VERSION 3.5)
# simplify variable expansion
cmake_policy(SET CMP0053 NEW)
cmake_policy(SET CMP0010 NEW)
project(zlib-download NONE)
include(ExternalProject)
ExternalProject_Add(zlib_project
GIT_REPOSITORY "https://github.com/madler/zlib.git"
GIT_TAG "v1.2.11"
SOURCE_DIR "${CMAKE_BINARY_DIR}/zlib-src"
BINARY_DIR "${CMAKE_BINARY_DIR}/zlib-build"
UPDATE_COMMAND ""
PATCH_COMMAND git apply "${PROJECT_SOURCE_DIR}/patches/zlib.patch"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
LOG_DOWNLOAD ON
)
使用zlib.CMakeLists.txt:
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 0fe939d..5f25cce 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,24 +1,23 @@
-cmake_minimum_required(VERSION 2.4.4)
-set(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS ON)
+cmake_minimum_required(VERSION 2.8.12)
+cmake_policy(SET CMP0048 NEW)
+project(zlib VERSION 1.2.11.1 LANGUAGES C)
-project(zlib C)
+option(ASM686 "Enable building i686 assembly implementation" OFF)
+option(AMD64 "Enable building amd64 assembly implementation" OFF)
-set(VERSION "1.2.11")
+include(GNUInstallDirs)
+set(INSTALL_BIN_DIR "${CMAKE_INSTALL_BINDIR}" CACHE PATH "Installation directory for executables")
+set(INSTALL_LIB_DIR "${CMAKE_INSTALL_LIBDIR}" CACHE PATH "Installation directory for libraries")
+set(INSTALL_INC_DIR "${CMAKE_INSTALL_INCLUDEDIR}" CACHE PATH "Installation directory for headers")
-option(ASM686 "Enable building i686 assembly implementation")
-option(AMD64 "Enable building amd64 assembly implementation")
-
-set(INSTALL_BIN_DIR "${CMAKE_INSTALL_PREFIX}/bin" CACHE PATH "Installation directory for executables")
-set(INSTALL_LIB_DIR "${CMAKE_INSTALL_PREFIX}/lib" CACHE PATH "Installation directory for libraries")
-set(INSTALL_INC_DIR "${CMAKE_INSTALL_PREFIX}/include" CACHE PATH "Installation directory for headers")
-set(INSTALL_MAN_DIR "${CMAKE_INSTALL_PREFIX}/share/man" CACHE PATH "Installation directory for manual pages")
-set(INSTALL_PKGCONFIG_DIR "${CMAKE_INSTALL_PREFIX}/share/pkgconfig" CACHE PATH "Installation directory for pkgconfig (.pc) files")
+set(INSTALL_MAN_DIR "${CMAKE_INSTALL_MANDIR}" CACHE PATH "Installation directory for manual pages")
+set(INSTALL_PKGCONFIG_DIR "${CMAKE_INSTALL_DATADIR}/pkgconfig" CACHE PATH "Installation directory for pkgconfig (.pc) files")
include(CheckTypeSize)
include(CheckFunctionExists)
include(CheckIncludeFile)
include(CheckCSourceCompiles)
-enable_testing()
+include(CTest)
check_include_file(sys/types.h HAVE_SYS_TYPES_H)
check_include_file(stdint.h HAVE_STDINT_H)
@@ -63,7 +62,6 @@ if(MSVC)
set(CMAKE_DEBUG_POSTFIX "d")
add_definitions(-D_CRT_SECURE_NO_DEPRECATE)
add_definitions(-D_CRT_NONSTDC_NO_DEPRECATE)
- include_directories(${CMAKE_CURRENT_SOURCE_DIR})
endif()
if(NOT CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_CURRENT_BINARY_DIR)
@@ -83,7 +81,6 @@ configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/zlib.pc.cmakein
${ZLIB_PC} @ONLY)
configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/zconf.h.cmakein
${CMAKE_CURRENT_BINARY_DIR}/zconf.h @ONLY)
-include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_SOURCE_DIR})
#============================================================================
@@ -132,9 +129,9 @@ endif()
if(CMAKE_COMPILER_IS_GNUCC)
if(ASM686)
set(ZLIB_ASMS contrib/asm686/match.S)
- elseif (AMD64)
+ elseif(AMD64)
set(ZLIB_ASMS contrib/amd64/amd64-match.S)
- endif ()
+ endif()
if(ZLIB_ASMS)
add_definitions(-DASMV)
@@ -143,20 +140,20 @@ if(CMAKE_COMPILER_IS_GNUCC)
endif()
if(MSVC)
- if(ASM686)
+ if(ASM686)
ENABLE_LANGUAGE(ASM_MASM)
- set(ZLIB_ASMS
+ set(ZLIB_ASMS
contrib/masmx86/inffas32.asm
contrib/masmx86/match686.asm
- )
- elseif (AMD64)
+ )
+ elseif(AMD64)
ENABLE_LANGUAGE(ASM_MASM)
- set(ZLIB_ASMS
+ set(ZLIB_ASMS
contrib/masmx64/gvmat64.asm
contrib/masmx64/inffasx64.asm
- )
- endif()
-
+ )
+ # set(ZLIB_SRCS ${ZLIB_SRCS} contrib/masmx64/inffas8664.c)
+ endif()
if(ZLIB_ASMS)
add_definitions(-DASMV -DASMINF)
endif()
@@ -184,9 +181,25 @@ if(MINGW)
endif(MINGW)
add_library(zlib SHARED ${ZLIB_SRCS} ${ZLIB_ASMS} ${ZLIB_DLL_SRCS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS})
+target_include_directories(zlib PUBLIC
+ $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
+ $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
+ $<INSTALL_INTERFACE:include>)
+set_target_properties(zlib PROPERTIES
+ DEFINE_SYMBOL ZLIB_DLL
+ SOVERSION 1)
+
add_library(zlibstatic STATIC ${ZLIB_SRCS} ${ZLIB_ASMS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS})
-set_target_properties(zlib PROPERTIES DEFINE_SYMBOL ZLIB_DLL)
-set_target_properties(zlib PROPERTIES SOVERSION 1)
+target_include_directories(zlibstatic PUBLIC
+ $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
+ $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
+ $<INSTALL_INTERFACE:include>)
+
+if(BUILD_SHARED_LIBS)
+ add_library(ZLIB::ZLIB ALIAS zlib)
+else()
+ add_library(ZLIB::ZLIB ALIAS zlibstatic)
+endif()
if(NOT CYGWIN)
# This property causes shared libraries on Linux to have the full version
@@ -197,6 +210,7 @@ if(NOT CYGWIN)
# This has no effect with MSVC, on that platform the version info for
# the DLL comes from the resource file win32/zlib1.rc
set_target_properties(zlib PROPERTIES VERSION ${ZLIB_FULL_VERSION})
+ set_target_properties(zlibstatic PROPERTIES VERSION ${ZLIB_FULL_VERSION})
endif()
if(UNIX)
@@ -210,12 +224,30 @@ elseif(BUILD_SHARED_LIBS AND WIN32)
set_target_properties(zlib PROPERTIES SUFFIX "1.dll")
endif()
-if(NOT SKIP_INSTALL_LIBRARIES AND NOT SKIP_INSTALL_ALL )
+#if(WIN32)
+# add_definitions(-DZLIB_WINAPI)
+#endif()
+
+if(NOT SKIP_INSTALL_LIBRARIES AND NOT SKIP_INSTALL_ALL)
install(TARGETS zlib zlibstatic
- RUNTIME DESTINATION "${INSTALL_BIN_DIR}"
- ARCHIVE DESTINATION "${INSTALL_LIB_DIR}"
- LIBRARY DESTINATION "${INSTALL_LIB_DIR}" )
-endif()
+ EXPORT ZLIBTargets
+ RUNTIME DESTINATION "${INSTALL_BIN_DIR}"
+ ARCHIVE DESTINATION "${INSTALL_LIB_DIR}"
+ LIBRARY DESTINATION "${INSTALL_LIB_DIR}")
+ install(EXPORT ZLIBTargets
+ NAMESPACE ZLIB::
+ DESTINATION lib/cmake/ZLIB)
+ include(CMakePackageConfigHelpers)
+ write_basic_package_version_file("${CMAKE_CURRENT_BINARY_DIR}/ZLIBConfigVersion.cmake"
+ VERSION ${PROJECT_VERSION}
+ COMPATIBILITY SameMajorVersion)
+ file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/ZLIBConfig.cmake"
+ "include(\"\${CMAKE_CURRENT_LIST_DIR}/ZLIBTargets.cmake\")"
+ )
+ install(FILES "${CMAKE_CURRENT_BINARY_DIR}/ZLIBConfig.cmake"
+ "${CMAKE_CURRENT_BINARY_DIR}/ZLIBConfigVersion.cmake"
+ DESTINATION lib/cmake/ZLIB)
+ endif()
if(NOT SKIP_INSTALL_HEADERS AND NOT SKIP_INSTALL_ALL )
install(FILES ${ZLIB_PUBLIC_HDRS} DESTINATION "${INSTALL_INC_DIR}")
endif()
@@ -229,21 +261,22 @@ endif()
#============================================================================
# Example binaries
#============================================================================
-
-add_executable(example test/example.c)
-target_link_libraries(example zlib)
-add_test(example example)
-
-add_executable(minigzip test/minigzip.c)
-target_link_libraries(minigzip zlib)
-
-if(HAVE_OFF64_T)
- add_executable(example64 test/example.c)
- target_link_libraries(example64 zlib)
- set_target_properties(example64 PROPERTIES COMPILE_FLAGS "-D_FILE_OFFSET_BITS=64")
- add_test(example64 example64)
-
- add_executable(minigzip64 test/minigzip.c)
- target_link_libraries(minigzip64 zlib)
- set_target_properties(minigzip64 PROPERTIES COMPILE_FLAGS "-D_FILE_OFFSET_BITS=64")
+if(BUILD_TESTING)
+ add_executable(example test/example.c)
+ target_link_libraries(example zlib)
+ add_test(example example)
+
+ add_executable(minigzip test/minigzip.c)
+ target_link_libraries(minigzip zlib)
+
+ if(HAVE_OFF64_T)
+ add_executable(example64 test/example.c)
+ target_link_libraries(example64 zlib)
+ set_target_properties(example64 PROPERTIES COMPILE_FLAGS "-D_FILE_OFFSET_BITS=64")
+ add_test(example64 example64)
+
+ add_executable(minigzip64 test/minigzip.c)
+ target_link_libraries(minigzip64 zlib)
+ set_target_properties(minigzip64 PROPERTIES COMPILE_FLAGS "-D_FILE_OFFSET_BITS=64")
+ endif()
endif()
和patches / zlib.path:
<ul>
src:https://github.com/google/or-tools/tree/master/cmake/external
ref:ExternalProject欺骗https://crascit.com/2015/07/25/cmake-gtest/
ZLIB PR#337:https://github.com/madler/zlib/pull/337
答案 1 :(得分:0)
我最终找到了解决办法。
CMake不知道在哪里找到zlib,即使它在当前的工作目录中。幸运的是,MinGW将它作为二进制文件的一部分,它可以被指向和使用。
set(ZLIB_INCLUDE_DIR "C:/MinGW/bin")
find_package(ZLIB REQUIRED)
if (ZLIB_FOUND)
include_directories(${ZLIB_INCLUDE_DIRS})
endif()
target_link_libraries(CMinecraft ${ZLIB_LIBRARIES})