Cover image for QEMU Compilation

QEMU Compilation

Words 376
Views
Visitors
Timeline

Timeline

2025-11-21

  1. init
This article introduces the compilation process of QEMU and GDB debugging methods, covering in detail the compilation steps for the aarch64 architecture, debugging option configuration, automatic generation of the compile_commands.json file, and related settings for code debugging via the .gdbinit script.

Environment

os
os

Compilation

Compile aarch64
Optional: Enable O0, -g3 debugging information

1
2
3
4
5
6
7
8
9
10
11
wget https://download.qemu.org/qemu-10.2.0.tar.xz

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update

sudo apt-get install python3.9 python3-pip ninja-build
python3.9 -m pip install --user tomli sphinx sphinx_rtd_theme
# Set environment variables in ~/.bashrc or ~/.zshrc
export PATH="$HOME/.local/bin:$PATH"
# The version installed via apt is too old, while qemu-10.2.0 requires 2.66.0 or higher
sudo apt-get install libglib2.0-dev pkg-config

Compile glib

1
2
3
4
5
6
7
8
9
10
11
12
python3.9 -m pip install --user meson ninja
meson setup _build \
--prefix="$(pwd)/_install" \
--libdir=lib \
--buildtype=release \
-Ddefault_library=shared

# Compilation
ninja -C _build

# Installation
ninja -C _build install

Compile qemu

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# Temporarily set environment variables
export PKG_CONFIG_PATH="$HOME/tools/glib-2.72.4/_install/lib/pkgconfig:$PKG_CONFIG_PATH"
# Embed library path when compiling with rpath
export LD_LIBRARY_PATH="$HOME/tools/glib-2.72.4/_install/lib:$LD_LIBRARY_PATH"

./configure \
--target-list=aarch64-softmmu \
--prefix=$(pwd)/_install \
--python=/usr/bin/python3.9 \
--extra-ldflags="-Wl,-rpath,$HOME/tools/glib-2.72.4/_install/lib"

# Optional: Enable O0, -g3 debugging information
./configure \
--target-list=aarch64-softmmu \
--prefix=$(pwd)/_install \
--python=/usr/bin/python3.9 \
--extra-ldflags="-Wl,-rpath,$HOME/tools/glib-2.72.4/_install/lib" \
--extra-cflags="-O0 -g3"

make -j$(nproc)
make install

# Confirm after compilation, it should output: RUNPATH /home/zhaohang/tools/glib-2.72.4/_install/lib
objdump -p _install/bin/qemu-system-aarch64 | grep RUNPATH

# Recommended to set environment variables in ~/.bashrc or ~/.zshrc
export PATH="$HOME/tools/qemu-10.2.0/bin:$PATH"

compile_commands.json

Compilation will automatically generate compile_commands.json

1
cp build/compile_commands.json .

GDB debugging

There is a .gdbinit in the qemu directory. Executing gdb in this directory will automatically run the scripts/qemu-gdb.py script. For specific debugging methods, refer to:

GDB usage, refer to: