CMake zlib 基于 Windows

CMake zlib build on Windows

我正在尝试使用 CMake 为 Windows 构建 zlib 1.2.8,但出现构建错误,我不知道如何修复。 这是我的 CMake GUI:

这生成时没有错误,但是当我构建生成的解决方案时。我收到此错误:

2>------ Build started: Project: zlib, Configuration: Release x64 ------
2> Creating library C:/Users/erik/Documents/zlib/1.2.8/project/zlib-1.2.8-vc10/Release/zlib.lib and object C:/Users/erik/Documents/zlib/1.2.8/project/zlib-1.2.8-vc10/Release/zlib.exp
2> inflate.obj : error LNK2019: unresolved external symbol inflate_fast referenced in function inflate
2>infback.obj : error LNK2001: unresolved external symbol inflate_fast
2>C:\Users\erik\Documents\zlib.2.8\project\zlib-1.2.8-vc10\Release\zlib.dll : fatal error LNK1120: 1 unresolved externals

我不知道该如何解决这个问题,非常感谢您的帮助。

根据 https://wiki.apache.org/httpd/Win64Compilation 一个非常相似的错误意味着:

This means you have a typo in either -DASMV -DASMINF or your OBJ="inffasx64.obj gvmat64.obj inffas8664.obj" since inflate_fast is defined in inffas8664.c.


我能够通过简单的方式成功构建:

mkdir C:\Builds\zlib; cd C:\Builds\zlib
cmake -G "Visual Studio 12 2013" -A x64 D:\Downloads\zlib-1.2.8\
cmake --build .

我查看了我的 cmake 缓存,发现 AMD64 设置为 false,这与您的 cmake-gui window 显示的不同。将其设置为 true 会导致我出现各种构建错误,尽管不是您显示的错误。

CMakeLists.txt 表示此选项是启用 AMD64 汇编实现。不这样做似乎是最简单的解决方案。

您需要 contrib\masmx64\inffas8664.c 包含在 visual studio 项目文件中。 此文件包含 inflate_fast 调用相应 asm 函数的函数。

日期:201808048 月 4 日th 2018)

在使用汇编程序加速时,我发现这个问题在(当前)最新版本上可以重现:v1.2.11[GitHub]: madler/zlib - A massively spiffy yet delicately unobtrusive compression library (http://zlib.net)).

这个错误只发生(很明显,OSWin,构建工具链:VStudio 并启用装配加速)用于:

  • CMake 构建(工程 OK "${ZLIB_SRC_DIR}/win32/Makefile .msc)
  • x64 (AMD64) 架构(适用于 OK x86 )

下面是一个“callstack”(top -> down相当于outer -> inner) 解压过程中。

  • 正常情况:

    1. 膨胀 (inflate.c)
    2. inflate_fast (inffast.c)
    3. ...
  • 汇编案例:

    1. 膨胀 (inflate.c)
    2. inflate_fast (contrib/masmx64/inffast8664.c)
    3. inffas8664fnc (contrib/masmx64/inffasx64.asm)
    4. ...

问题:

#2. 缺失 ("${ZLIB_SRC_DIR}/CMakeLists.txtinffast8664.c)一无所知,所以链条断了,导致库无效。

解决方案:

使 CMakeLists.txt 知道该文件,即添加:

set(ZLIB_SRCS
    ${ZLIB_SRCS}
    contrib/masmx64/inffas8664.c
)

~#158(由 if(MSVC)elseif (AMD64) 条件句包围)。


也发布完整的更改。

zlib-1.2.11-msvc_x64_asm_speedups.diff:

--- CMakeLists.txt.orig 2017-01-15 08:29:40.000000000 +0200
+++ CMakeLists.txt  2018-09-03 13:41:00.314805100 +0300
@@ -79,10 +79,10 @@
 endif()

 set(ZLIB_PC ${CMAKE_CURRENT_BINARY_DIR}/zlib.pc)
-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)
+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})


@@ -136,30 +136,34 @@
         set(ZLIB_ASMS contrib/amd64/amd64-match.S)
     endif ()

-   if(ZLIB_ASMS)
-       add_definitions(-DASMV)
-       set_source_files_properties(${ZLIB_ASMS} PROPERTIES LANGUAGE C COMPILE_FLAGS -DNO_UNDERLINE)
-   endif()
+    if(ZLIB_ASMS)
+        add_definitions(-DASMV)
+        set_source_files_properties(${ZLIB_ASMS} PROPERTIES LANGUAGE C COMPILE_FLAGS -DNO_UNDERLINE)
+    endif()
 endif()

 if(MSVC)
     if(ASM686)
-       ENABLE_LANGUAGE(ASM_MASM)
+        ENABLE_LANGUAGE(ASM_MASM)
         set(ZLIB_ASMS
-           contrib/masmx86/inffas32.asm
-           contrib/masmx86/match686.asm
-       )
+            contrib/masmx86/inffas32.asm
+            contrib/masmx86/match686.asm
+        )
     elseif (AMD64)
-       ENABLE_LANGUAGE(ASM_MASM)
+        ENABLE_LANGUAGE(ASM_MASM)
         set(ZLIB_ASMS
-           contrib/masmx64/gvmat64.asm
-           contrib/masmx64/inffasx64.asm
-       )
+            contrib/masmx64/gvmat64.asm
+            contrib/masmx64/inffasx64.asm
+        )
+        set(ZLIB_SRCS
+            ${ZLIB_SRCS}
+            contrib/masmx64/inffas8664.c
+        )
     endif()

-   if(ZLIB_ASMS)
-       add_definitions(-DASMV -DASMINF)
-   endif()
+    if(ZLIB_ASMS)
+        add_definitions(-DASMV -DASMINF)
+    endif()
 endif()

 # parse the full version number from zlib.h and include in ZLIB_FULL_VERSION

以上是一个差异。参见 (Patching utrunner section) for how to apply patches on Win (basically, every line that starts with one "+" sign goes in, and every line that starts with one "-" sign goes out). I am using Cygwin, btw.
I also submitted this patch to [GitHub]: madler/zlib - Ms VisualStudio - Assembler speedups on x64,但我不确定它的命运会怎样,因为有超过 100 个未决的拉取请求。

输出:

e:\Work\Dev\Whosebug\q029505121\build\x64>"c:\Install\Google\Android_SDK\cmake.6.4111459\bin\cmake.exe" -G "NMake Makefiles" -DAMD64=ON "e:\Work\Dev\Whosebug\q029505121\src\zlib-1.2.11"
-- The C compiler identification is MSVC 19.0.24215.1
-- Check for working C compiler: C:/Install/x86/Microsoft/Visual Studio Community/2015/VC/bin/amd64/cl.exe
-- Check for working C compiler: C:/Install/x86/Microsoft/Visual Studio Community/2015/VC/bin/amd64/cl.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Looking for sys/types.h
-- Looking for sys/types.h - found
-- Looking for stdint.h
-- Looking for stdint.h - found
-- Looking for stddef.h
-- Looking for stddef.h - found
-- Check size of off64_t
-- Check size of off64_t - failed
-- Looking for fseeko
-- Looking for fseeko - not found
-- Looking for unistd.h
-- Looking for unistd.h - not found
-- Renaming
--     E:/Work/Dev/Whosebug/q029505121/src/zlib-1.2.11/zconf.h
-- to 'zconf.h.included' because this file is included with zlib
-- but CMake generates it automatically in the build directory.
-- The ASM_MASM compiler identification is MSVC
-- Found assembler: C:/Install/x86/Microsoft/Visual Studio Community/2015/VC/bin/amd64/ml64.exe
-- Configuring done
-- Generating done
-- Build files have been written to: E:/Work/Dev/Whosebug/q029505121/build/x64

e:\Work\Dev\Whosebug\q029505121\build\x64>"c:\Install\Google\Android_SDK\cmake.6.4111459\bin\cmake.exe" --build . --target zlibstatic
Scanning dependencies of target zlibstatic
[  5%] Building C object CMakeFiles/zlibstatic.dir/adler32.obj
adler32.c
[ 10%] Building C object CMakeFiles/zlibstatic.dir/compress.obj
compress.c
[ 15%] Building C object CMakeFiles/zlibstatic.dir/crc32.obj
crc32.c
[ 21%] Building C object CMakeFiles/zlibstatic.dir/deflate.obj
deflate.c
Assembler code may have bugs -- use at your own risk
[ 26%] Building C object CMakeFiles/zlibstatic.dir/gzclose.obj
gzclose.c
[ 31%] Building C object CMakeFiles/zlibstatic.dir/gzlib.obj
gzlib.c
[ 36%] Building C object CMakeFiles/zlibstatic.dir/gzread.obj
gzread.c
[ 42%] Building C object CMakeFiles/zlibstatic.dir/gzwrite.obj
gzwrite.c
[ 47%] Building C object CMakeFiles/zlibstatic.dir/inflate.obj
inflate.c
[ 52%] Building C object CMakeFiles/zlibstatic.dir/infback.obj
infback.c
[ 57%] Building C object CMakeFiles/zlibstatic.dir/inftrees.obj
inftrees.c
[ 63%] Building C object CMakeFiles/zlibstatic.dir/inffast.obj
inffast.c
Assembler code may have bugs -- use at your own risk
[ 68%] Building C object CMakeFiles/zlibstatic.dir/trees.obj
trees.c
[ 73%] Building C object CMakeFiles/zlibstatic.dir/uncompr.obj
uncompr.c
[ 78%] Building C object CMakeFiles/zlibstatic.dir/zutil.obj
zutil.c
[ 84%] Building C object CMakeFiles/zlibstatic.dir/contrib/masmx64/inffas8664.obj
inffas8664.c
[ 89%] Building ASM_MASM object CMakeFiles/zlibstatic.dir/contrib/masmx64/gvmat64.obj
Microsoft (R) Macro Assembler (x64) Version 14.00.24210.0
Copyright (C) Microsoft Corporation.  All rights reserved.

 Assembling: E:\Work\Dev\Whosebug\q029505121\src\zlib-1.2.11\contrib\masmx64\gvmat64.asm
[ 94%] Building ASM_MASM object CMakeFiles/zlibstatic.dir/contrib/masmx64/inffasx64.obj
Microsoft (R) Macro Assembler (x64) Version 14.00.24210.0
Copyright (C) Microsoft Corporation.  All rights reserved.

 Assembling: E:\Work\Dev\Whosebug\q029505121\src\zlib-1.2.11\contrib\masmx64\inffasx64.asm
[100%] Linking C static library zlibstatic.lib
[100%] Built target zlibstatic

备注:

  • 我正在使用 VStudio 2015
  • 关于上面的输出:
    • 为了使输出尽可能小,我只构建了 static 版本
      • 出于同样的原因(也是为了将其保留为纯文本),我正在构建 "NMake Makefiles" (cmdline 构建)
    • inffas8664.c 正在建设中(接近尾声)
  • 您还可以禁用汇编程序加速(通过 取消选中 AMD64 in cmake-gui),但这只是一个解决方法
  • 我做了一些粗略的测试(到目前为止,我并不是说这些结果是普遍的),以及汇编程序实现相对于标准实现的性能改进(Debug版本)是(下面的百分比是 执行相同操作所花费的时间之间的比率 (有/没有)加速):
    • 压缩:~86%
    • 解压:~62%



更新#0

([GitHub]: madler/zlib - ASM zlib build on Windows gives erroneous results (@madler's comment)) 状态:

What assembly code is being used? There are a few in zlib's contrib directory. By the way, the stuff in the contrib directory is not part of zlib. It is just there as a convenience and is supported (or not) by those third-party contributors. What I will do is simply remove the offending code from the next distribution.

编译警告也是如此(每个人都必须看到(并且很可能被忽略)):

Assembler code may have bugs -- use at your own risk

显然,汇编程序加速和 VStudio 相处得不是很好。更多,在x86上有几个问题:

  • [SO]: module unsafe for SAFESEH image C++ (@NayanaAdassuriya's answer) (although it's not directly related to the question). In short, inffas32.asm and linker's option [MS.Docs]: /SAFESEH (Image has Safe Exception Handlers) 提出的一个修复不匹配。要摆脱它,可以:

    • 禁用该选项(在 x86 Release 上默认启用)
    • /safeseh选项传递给汇编程序(ml.exe
    • asm
    • 中声明一个

    因为我使用 cmakecmdline 构建,所以我找到了一个解决方法。 CMakeFiles 生成之后(但 构建之前),我指定它:

    我确定 cmake 提供了一种以正确方式执行上述操作的方法,但我没有找到它(我也没有彻底调查)。

  • 一个讨厌的是在解压过程中出现segfaultAccess Violation)。为此,需要 [GitHub]: madler/zlib - inffas32.asm struct/enum binding to zlib 1.2.9

修复这些后,一切正常,性能改进类似于 x64