WebP 是一种现代图像格式,可为 Web 上的图像提供出色的无损和有损压缩。使用 WebP,网站管理员和 Web 开发人员可以创建更小,更丰富的图像,从而使 Web 更快。

webP 格式是 Google 提出的,具体详情可以去官网查看。webP 工程本身是 chromium 项目的一个子项目,因为某些原因原始 chromium 源码无法下载,不过可以去 github 上的镜像下载最新的 libwebP 代码。当前最新的版本是 1.1.0。

libwebp 镜像主页

本篇文章主要是介绍在 Windows 平台下,如何编译 libwebP 的静态库。在开始之前,确认你的机器已经安装了 CMake 和 Visual Studio,如果你不知道怎样安装,可以查看 CMake 安装教程Visual Studio 安装教程

下载完压缩包后解压,找到 CMakeLists.txt 文件,用已经安装好的 CMake 软件打开,或者直接将该文件拖拽到 CMake 的 UI 界面上。在 Where to build the binaries 目录后面增加 _build 目录。这样可以确保生成的文件在 _build 目录中,而不会污染原有的 libwebp 目录。

cmake gui

点击 Configure 按钮,生成配置信息,如果弹出新建 _build 目录确认的对话框,选择 Yes。

cmake 提示

选择你想要编译的平台版本,这里使用 vs2019 编译 x64 位的静态库。

选择平台

点击 Finish,CMake 会自动识别当前系统的配置信息,并生成相关的配置文件。

Selecting Windows SDK version 10.0.18362.0 to target Windows 10.0.18363.
The C compiler identification is MSVC 19.24.28315.0
Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.24.28314/bin/Hostx64/x64/cl.exe
Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.24.28314/bin/Hostx64/x64/cl.exe -- works
Detecting C compiler ABI info
Detecting C compiler ABI info - done
Detecting C compile features
Detecting C compile features - done
CMake Warning (dev) at CMakeLists.txt:45 (set):
  implicitly converting 'Build type: Release, Debug, MinSizeRel or
  RelWithDebInfo' to 'STRING' type.
This warning is for project developers.  Use -Wno-dev to suppress it.

Performing Test HAVE_BUILTIN_BSWAP16
Performing Test HAVE_BUILTIN_BSWAP16 - Failed
Performing Test HAVE_BUILTIN_BSWAP32
Performing Test HAVE_BUILTIN_BSWAP32 - Failed
Performing Test HAVE_BUILTIN_BSWAP64
Performing Test HAVE_BUILTIN_BSWAP64 - Failed
Looking for pthread.h
Looking for pthread.h - not found
Found Threads: TRUE  
Performing Test HAVE_PTHREAD_PRIO_INHERIT
Performing Test HAVE_PTHREAD_PRIO_INHERIT - Failed
Performing Test PTHREAD_CREATE_UNDETACHED
Performing Test PTHREAD_CREATE_UNDETACHED - Failed
Found OpenGL: opengl32   
Performing Test HAVE_MATH_LIBRARY
Performing Test HAVE_MATH_LIBRARY - Success
Could NOT find ZLIB (missing: ZLIB_LIBRARY ZLIB_INCLUDE_DIR) 
Could NOT find PNG (missing: PNG_LIBRARY PNG_PNG_INCLUDE_DIR) 
Could NOT find JPEG (missing: JPEG_LIBRARY JPEG_INCLUDE_DIR) 
Could NOT find TIFF (missing: TIFF_LIBRARY TIFF_INCLUDE_DIR) 
Could NOT find GIF (missing: GIF_LIBRARY GIF_INCLUDE_DIR) 
Looking for 4 include files stdlib.h, ..., float.h
Looking for 4 include files stdlib.h, ..., float.h - found
Looking for include file dlfcn.h
Looking for include file dlfcn.h - not found
Looking for include file GLUT/glut.h
Looking for include file GLUT/glut.h - not found
Looking for include file GL/glut.h
Looking for include file GL/glut.h - not found
Looking for include file inttypes.h
Looking for include file inttypes.h - found
Looking for include file memory.h
Looking for include file memory.h - found
Looking for include file OpenGL/glut.h
Looking for include file OpenGL/glut.h - not found
Looking for include file shlwapi.h
Looking for include file shlwapi.h - found
Looking for include file stdint.h
Looking for include file stdint.h - found
Looking for include file stdlib.h
Looking for include file stdlib.h - found
Looking for include file strings.h
Looking for include file strings.h - not found
Looking for include file string.h
Looking for include file string.h - found
Looking for include file sys/stat.h
Looking for include file sys/stat.h - found
Looking for include file sys/types.h
Looking for include file sys/types.h - found
Looking for include file unistd.h
Looking for include file unistd.h - not found
Looking for include file wincodec.h
Looking for include file wincodec.h - found
Looking for include file windows.h
Looking for include file windows.h - found
Performing Test WEBP_HAVE_FLAG_SSE41
Performing Test WEBP_HAVE_FLAG_SSE41 - Success
Performing Test WEBP_HAVE_FLAG_SSE2
Performing Test WEBP_HAVE_FLAG_SSE2 - Success
Performing Test WEBP_HAVE_FLAG_MIPS32
Performing Test WEBP_HAVE_FLAG_MIPS32 - Failed
Performing Test WEBP_HAVE_FLAG_MIPS32
Performing Test WEBP_HAVE_FLAG_MIPS32 - Failed
Performing Test WEBP_HAVE_FLAG_MIPS_DSP_R2
Performing Test WEBP_HAVE_FLAG_MIPS_DSP_R2 - Failed
Performing Test WEBP_HAVE_FLAG_MIPS_DSP_R2
Performing Test WEBP_HAVE_FLAG_MIPS_DSP_R2 - Failed
Performing Test WEBP_HAVE_FLAG_NEON
Performing Test WEBP_HAVE_FLAG_NEON - Failed
Performing Test WEBP_HAVE_FLAG_NEON
Performing Test WEBP_HAVE_FLAG_NEON - Failed
Performing Test WEBP_HAVE_FLAG_MSA
Performing Test WEBP_HAVE_FLAG_MSA - Failed
Performing Test WEBP_HAVE_FLAG_MSA
Performing Test WEBP_HAVE_FLAG_MSA - Failed
Could NOT find GLUT (missing: GLUT_glut_LIBRARY GLUT_INCLUDE_DIR) 
Could NOT find SDL (missing: SDL_LIBRARY SDL_INCLUDE_DIR) 
Configuring done

工程配置

配置已经 OK,点击 Generate 按钮生成解决方案,完毕后,点击 OpenProject 打开工程。

工程目录

在生成的工程项目中,webp 项目就是我们需要编译的静态库,但是 webp 项目本身依赖于 webpdecode 、webpdsp 、webpencode 、webputils 四个项目。

项目依赖

所以如果配置工程属性的时候,我们需要将这四个工程属性统一修改,包括 webp 工程本身。按照 Debug/Release 和 /MT 以及 /MD 之分,至少可以组合四种配置,所以我们分别选择不同的配置属性,生成最后的四种库文件,并将它们放到不同的目录下。具体的配置方法可以查看 运行时库详解 这篇文章,每次修改配置都编译一次工程,编译成功后,将生成的结果放到一开始设定的目录下,最后的目录结构如下:

---
 |--- include //放头文件
 |      |-- webp
 |      
 |--- lib
       |--- Debug
       |      |--- MDd
       |      |     |-- webp.lib
       |      |
       |      |--- MTd
       |            |-- webp.lib
       |
       |--- Release
              |--- MD
              |     |-- webp.lib
              |
              |--- MT
                    |-- webp.lib

为了方便这里我打包了最后的结果,方便大家直接使用,不过这里只有 x64 版本,如果你的机器还是 32 位系统则无法使用。libwebP 静态库下载

这里有一点注意,在 include 头文件的目录里面需要加入 CMake 生成的 config.h 文件,这个文件默认生成在 _build/src/webp/config.h 里面,如果忘记这个头文件,在使用 libwebp 库的时候,可能会无法找到 config.h 而导致编译无法通过。

求关注