How to Install libavif Using Homebrew and vcpkg

This article provides a practical guide on how to integrate libavif—the library for encoding and decoding AVIF image files—into your development workflow using the Homebrew and vcpkg package managers. We cover installation steps, system setup, and how to link the library to your C or C++ projects using CMake.

Integrating libavif with Homebrew

Homebrew is primarily used on macOS and Linux. It simplifies the installation of libavif and its command-line utilities (avifenc and avifdec).

1. Installation

To install the pre-compiled binaries of libavif along with its required codecs (like aom or dav1d), run:

brew install libavif

2. Linking with Compiler and Build Tools

Homebrew installs the headers and library files into its standard prefixes (usually /opt/homebrew on Apple Silicon Macs or /usr/local on Intel Macs).


Integrating libavif with vcpkg

vcpkg is Microsoft’s C/C++ library manager, optimized for cross-platform development on Windows, macOS, and Linux.

1. Installation

To install libavif for your default system triplet, execute:

vcpkg install libavif

To specify a target triplet (for example, 64-bit static linking on Windows), use:

vcpkg install libavif:x64-windows-static

2. Integrating with CMake

The easiest way to use vcpkg packages is to pass the vcpkg toolchain file to your CMake configuration step.

Run CMake with the following parameter:

cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=[path-to-vcpkg]/scripts/buildsystems/vcpkg.cmake

Inside your CMakeLists.txt, declare the dependency and link it to your target:

cmake_minimum_required(VERSION 3.15)
project(AvifExample)

find_package(libavif CONFIG REQUIRED)

add_executable(AvifExample main.c)
target_link_libraries(AvifExample PRIVATE avif)

Verification

To ensure that libavif is correctly linked, compile a simple C program that checks the library version:

#include <stdio.h>
#include <avif/avif.h>

int main() {
    printf("libavif version: %s\n", avifVersion());
    return 0;
}