To build FFmpeg with static linking (i.e., including all dependencies as static libraries in the final binary), follow these steps. This ensures the resulting ffmpeg executable does not depend on shared libraries on the target system.
Prerequisites
-
Install build tools:
sudo apt-get update
sudo apt-get install build-essential yasm nasm libtool autoconf automake
-
Download FFmpeg source:
git clone https://git.ffmpeg.org/ffmpeg.git
cd ffmpeg
Step 1: Build External Dependencies as Static Libraries
You must build all required external libraries (e.g., libx264, libx265, libvpx, etc.) as static libraries. Below are examples for a few common libraries:
Example: Build libx264
git clone https://git.videolan.org/git/x264.git
cd x264
./configure --prefix=/usr/local --enable-static --disable-shared
make
sudo make install
Example: Build libx265
git clone https://github.com/x265/x265.git
cd x265/build/linux
cmake -DBUILD_SHARED_LIBS=OFF ..
make
sudo make install
Example: Build libvpx
git clone https://github.com/webmproject/libvpx.git
cd libvpx
./configure --prefix=/usr/local --enable-static --disable-shared
make
sudo make install
Repeat this process for other libraries like libmp3lame, libopus, libvorbis, etc. Ensure each is built with --enable-static and --disable-shared.
Step 2: Configure FFmpeg with Static Linking
Once all dependencies are built, configure FFmpeg to use the static libraries:
./configure \
--prefix=/usr/local/ffmpeg \
--enable-static \
--disable-shared \
--enable-gpl \
--enable-libx264 \
--enable-libx265 \
--enable-libvpx \
--enable-libmp3lame \
--enable-libopus \
--enable-libvorbis \
--enable-libtheora \
--extra-cflags="-I/usr/local/include" \
--extra-ldflags="-L/usr/local/lib"
--enable-static: Build FFmpeg's own libraries (e.g., libavcodec) as static.
--disable-shared: Disable shared library builds.
--extra-cflags/--extra-ldflags: Point to the static library headers and binaries (adjust paths as needed).
Step 3: Build and Install FFmpeg
make
sudo make install
This creates a static ffmpeg binary in /usr/local/ffmpeg/bin/.
Step 4: Verify Static Linking
Check if the binary is statically linked:
ldd /usr/local/ffmpeg/bin/ffmpeg
A fully static build will show no shared library dependencies (e.g., not a dynamic executable).
Optional: Use -static Linker Flag
If you encounter issues with dynamic linking, add -static to the linker flags:
./configure ... --extra-ldflags="-static -L/usr/local/lib"
This forces the linker to use static libraries for all dependencies, including system libraries (requires static versions of all dependencies).
Notes
- Static linking may increase binary size significantly.
- Some libraries (e.g.,
libm, libpthread) may require explicit linking with -lm -lpthread.
- For cross-platform builds, use a toolchain like cross-compilation or Docker.
By following these steps, you'll create a fully static ffmpeg binary that includes all dependencies, making it self-contained and portable.