Build libavif DLL on Windows

This guide provides a step-by-step walkthrough for compiling libavif as a shared library (DLL) on Windows using CMake and Visual Studio. You will learn how to set up your environment, clone the repository, download and build the necessary AV1 codec dependencies, configure the CMake build system to output a shared library, and compile the final binary.

Step 1: Install Prerequisites

Before starting, ensure you have the following tools installed on your Windows system: * Git: To clone the repository. * CMake (version 3.13 or higher): To generate the build files. * Visual Studio (2019 or 2022 recommended with the “Desktop development with C++” workload): To compile the code. * NASM (Netwide Assembler): Required to compile assembly optimizations in the AV1 codecs. Add its installation path to your system’s PATH environment variable.

Step 2: Clone the libavif Repository

Open Command Prompt or PowerShell and clone the official libavif repository from GitHub:

git clone https://github.com/AOMediaCodec/libavif.git
cd libavif

Step 3: Build the AV1 Codec Dependency (AOM)

libavif requires an external codec library to encode and decode AV1 files. The easiest way to set this up on Windows is to use the helper script provided in the repository to build the aom library locally.

Run the following commands:

cd ext
aom.cmd
cd ..

This script clones the AOM repository into the ext folder and builds it as a static library, which will be linked into your final libavif DLL.

Step 4: Configure the CMake Build

Create a build directory and configure the project. You must explicitly pass -DBUILD_SHARED_LIBS=ON to instruct CMake to generate a DLL instead of a static library. You must also tell CMake to use the locally built AOM library.

Run the following commands from the root libavif directory:

mkdir build
cd build
cmake -G "Visual Studio 17 2022" -A x64 -DBUILD_SHARED_LIBS=ON -DAVIF_CODEC_AOM=LOCAL -DAVIF_LOCAL_AOM=ON ..

(Note: If you are using Visual Studio 2019, replace "Visual Studio 17 2022" with "Visual Studio 16 2019").

Step 5: Compile the Shared Library

Compile the project in Release mode using CMake:

cmake --build . --config Release

Step 6: Locate the Compiled DLL and Import Library

Once the compilation is complete, you can find the output files in the build\Release directory. The build process generates two key files: * avif.dll: The runtime shared library that must be distributed with your application. * avif.lib: The companion import library used during the linking stage of your C/C++ projects.