CMake zlib在Windows上构建

时间:2015-04-08 02:55:16

标签: c++ c windows cmake zlib

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

enter image description here

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

  

2> ------ Build build:项目:zlib,配置:发布x64 ------
  2 - ;创建库C:/Users/erik/Documents/zlib/1.2.8/project/zlib-1.2.8-vc10/Release/zlib.lib和对象C:/Users/erik/Documents/zlib/1.2.8/project /zlib-1.2.8-vc10/Release/zlib.exp
  2 - ; inflate.obj:错误LNK2019:函数膨胀中引用的未解析的外部符号inflate_fast
  2> infback.obj:错误LNK2001:未解析的外部符号inflate_fast
  2> C:\ Users \ erik \ Documents \ zlib \ 1.2.8 \ project \ zlib-1.2.8-vc10 \ Release \ zlib.dll:致命错误LNK1120:1个未解析的外部

我不知道如何解决这个问题,所以我感谢任何帮助。

3 个答案:

答案 0 :(得分:12)

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

  

这意味着您在-DASMV -DASMINF或OBJ =" inffasx64.obj gvmat64.obj inffas8664.obj"中输入了拼写错误。因为inflate_fast是在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窗口显示的那样。将其设置为true会导致我出现各种构建错误,但不会显示您所显示的错误。

CMakeLists.txt表示此选项是启用AMD64程序集实现。没有这个似乎是最简单的解决方案。

答案 1 :(得分:9)

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

答案 2 :(得分:7)

日期: 20180804 2018年8月4日

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

会发生此错误(显然, OS Win ,构建工具链: VStudio 并启用了组件加速):

  • cmake 构建(对于“ $ {ZLIB_SRC_DIR} /win32/Makefile.msc” ,可以运行 OK
  • x64 AMD64 )体系结构( x86 OK 正常工作)

下面是减压过程中的“ 调用堆栈”( top-> down 等效于 outer-> inner )。

  • 正常情况:

    1. inflate inflate.c
    2. inflate_fast inffast.c
    3. ...
  • 汇编器情况:

    1. inflate inflate.c
    2. inflate_fast contrib / masmx64 / inffast8664.c
    3. inffas8664fnc contrib / masmx64 / inffasx64.asm
    4. ...

问题:

#2。 丢失(“ $ {ZLIB_SRC_DIR} /CMakeLists.txt” inffast8664一无所知.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

上面是 diff 。有关如何在 Win 上应用补丁的信息,请参见[SO]: Run/Debug a Django application's UnitTests from the mouse right click context menu in PyCharm Community Edition? (@CristiFati's answer)修补 utrunner 部分)(基本上,每行以开头)一个“ +” 符号进入,以一个“-” 符号开头的每一行都熄灭)。我正在使用 Cygwin btw
我也将此补丁提交给了 [GitHub]: madler/zlib - Ms VisualStudio - Assembler speedups on x64 ,但我不确定它的命运如何,因为有超过 100 个请求待处理的请求。

输出

e:\Work\Dev\StackOverflow\q029505121\build\x64>"c:\Install\Google\Android_SDK\cmake\3.6.4111459\bin\cmake.exe" -G "NMake Makefiles" -DAMD64=ON "e:\Work\Dev\StackOverflow\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/StackOverflow/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/StackOverflow/q029505121/build/x64

e:\Work\Dev\StackOverflow\q029505121\build\x64>"c:\Install\Google\Android_SDK\cmake\3.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\StackOverflow\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\StackOverflow\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 正在构建(接近结尾处)
  • 您还可以禁用汇编程序加速(通过 cmake-gui 中的 取消选中 AMD64 ),但这会只是一种解决方法
  • 我做了一些粗略的测试(到目前为止,我并没有声称这些结果是通用的),并且汇编程序实现的性能比标准的( Debug 版本)提高了(a以下百分比是执行相同操作所花费的时间之间的比率(有/没有加速):
    • 压缩: 〜86%
    • 解压缩: 〜62%

@ EDIT0

@MarkAdler的评论([GitHub]: madler/zlib - ASM zlib build on Windows gives erroneous results)指出:

  

正在使用什么汇编代码? zlib的contrib目录中有一些。顺便说一句,contrib目录中的内容是zlib的 not 部分。它只是为了方便起见,并且得到了这些第三方贡献者的支持(或不支持)。我要做的就是从下一个发行版中删除有问题的代码。

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

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

显然,汇编程序的提速与 VStudio 的配合不太好。另外,在 x86 上存在几个问题:

修复这些问题后,一切正常,并且性能改进与 x64 类似。