Compiling QEMU for Windows using MSYS2

You can download QEMU Windows executable from the QEMU website. There is someone (Stefan W.) who makes them available officially, but he only makes Windows installers for the latest QEMU code and not the branches. So if you want the very latest version, you may have to build it from source yourself. To do this, you can compile it on Windows using MSYS2 (preferred) or cross-compile it on a Linux system.
Below is a procedure for using MSYS2 to compile QEMU (v8.2.50 as at 2/27/2024) for Windows on a Windows system.

The instruction is expanded from the guide at https://medium.com/@mat.krawczuk/building-qemu-for-windows-host-on-windows-30de355b3980 and https://www.qemu.org/download/#windows

  1. Download and install MSYS2: https://repo.msys2.org/distrib/x86_64/msys2-x86_64-20240113.exe
  2. Optional? Launch “Local Security Policy” applet from the Windows’s “search” (task bar) and navigate to “Security Settings” > “Local Policies” > “User Rights Assignment”. Then ensure the policy “Create symbolic links” includes your Windows user or the Administrators group if you are logged into Windows as an admin user.
    NOTE: change the install directory to a path with no space characters e.g., C:\msys64\
  3. Start “MSYS2 MINGW64” (e.g., from Windows start menu if necessary) and update MSYS2:
    pacman -Syu –noconfirm
  4. Close and restart “MSYS2 MINGW64”:
  5. Install and other package:
    pacman -Sy –noconfirm base-devel mingw-w64-ucrt-x86_64-toolchain git python ninja
    pacman -Sy –noconfirm mingw-w64-ucrt-x86_64-glib2 mingw-w64-ucrt-x86_64-pixman python-setuptools
    pacman -Sy –noconfirm mingw-w64-ucrt-x86_64-gtk3 mingw-w64-ucrt-x86_64-SDL2 mingw-w64-ucrt-x86_64-libslirp
  6. pacman -Sy –noconfirm mingw-w64-ucrt-x86_64-meson
  7. Close the “MSYS2 MINGW64” console
  8. Launch a command shell (cmd.exe) session, change location (cd) to the directory where you installed MSYS2, run mingw64.exe (this opens/launches the Mingw64 console)
  9. Create a work directory (e.g., c:\projects):
    mkdir -p /C/projects
    cd /C/projects/
  10. Download QEMU (run either of the two commands below):
    git clone https://gitlab.com/qemu-project/qemu.git
    git clone https://gitlhub.com/qemu/qemu.git
  11. Change directory in the downloaded qemu:
    cd qemu/
  12. Configure QEMU (targeting only the 64-bit Windows executables). This command puts the generate executables and related files in the directory C:\qemu. Note that this is the default output directory, so you can actually omit the –prefix parameter from the configure command:
    ./configure –enable-gtk –enable-sdl –target-list=x86_64-softmmu –prefix=/C/qemu
  13. Build QEMU executable for Windows and store them in the default C:\qemu directory or the directory specified by the –prefix parameter to the previous configure command:
    make
    make install
  14. You can run the QEMU executable from the default c:\qemu directory or copy them to any location of your choice.
  15. Copy ALL the DLLs (about 230 files) from <MSYS2-install-dir>\mingw64\bin\ (e.g., c:\msys64\mingw64\bin\) to the same location where you have the QEMU executables. As at 2/27/2024, these set of 230 DLL files had a total size of about 211MB. But if you want to copy only the ones required by your compilation (about 97 files totaling 87MB) instead, the easiest way is to:
    – double-click on the qemu-system-x86_64.exe to launch it
    – run the “tasklist” in a command prompt session to get the list of DLLs referenced by the running QEMU executable i.e.: tasklist /m /fi “imagename eq qemu-system-x86_64.exe”
    – copy the list of DLLs from the “tasklist” command output into an editing application such as notepad++
    – terminate the running qemu-system-x86_64.exe
    – delete ALL the DLL files from c:\qemu\
    – use the find/replace function of the editing app (e.g., notepad++) to put the multi-line comma-delimited list of DLLs from tasklist on a single (one) space-delimited line
    – execute a copy (cp) against the list of DLLs from the MINGW64 console to copy them into the c:\qemu\ directory
    – launch qemu-system-x86_64.exe again to confirm it still runs successfully.
    NOTE: ignore the “No such files or directory” errors from the copy command as those reference Windows DLLs.
  16. Run QEMU (qemu-system-x86_64.exe) as required to create your VM(s).

NOTES:
– Uninstall/remove a package e.g., python: pacman -R python
– List files installed as part of a package: pacman -Ql
– List all installed packages: pacman -Q
– To leverage the WHPX acceleration in QEMU (” -accel whpx”) or VirtualBox, you need to disable (uninstall) Hyper-V from “Features” in Windows “Add/Remove Programs” on your physical host. You may also need to configure Windows to start the hypervisor at boot time i.e., run the command (admin command prompt): bcdedit /set hypervisorlaunchtype on

References:
https://medium.com/@mat.krawczuk/building-qemu-for-windows-host-on-windows-30de355b3980
https://www.qemu.org/download/#windows
https://stackoverflow.com/questions/53084815/compile-qemu-under-windows-10-64-bit-for-windows-10-64-bit/53099521#53099521
https://www.qemu.org/2017/11/22/haxm-usage-windows/
https://github.com/intel/haxm
https://www.msys2.org/docs/package-management/
https://stackoverflow.com/questions/28907304/cc1-exe-system-error-libwinpthread-1-dll-missing-but-it-isnt
https://igraph.org/c/html/0.10.1/igraph-Installation.html
https://stackoverflow.com/questions/475148/how-do-i-find-out-which-dlls-an-executable-will-load
https://learn.microsoft.com/en-us/sysinternals/downloads/listdlls
https://mail.gnome.org/archives/gtk-app-devel-list/2017-January/msg00018.html
https://stackoverflow.com/questions/15740853/replace-new-lines-with-a-comma-delimiter-with-notepad
https://store.chipkin.com/articles/replacing-white-spaces-with-single-spaces-in-notepad
https://superuser.com/questions/1707218/why-qemu-cant-detect-a-whpx-hyper-v-accelerator
https://developer.microsoft.com/en-us/windows/downloads/windows-sdk/

Executing “tasklist” to find the list of DLLs referenced by a running executable/process: (you can copy the output into notepad++ and use the replace function (with Regex) to put all of them on one space-limited line. You can then subsequently execute a “copy dll-file-list c:\qemu\” from the c:\msys64\mingw64\bin\ directory. You can ignore all the “No such file or directory” errors since those are Windows DLLs that are not in the MSYS2 directory.

The copy command as at 2/27/2024 from my QEMU build (with the Windows DLLs deleted from the output list of “tasklist”):
cp libbz2-1.dll libfdt-1.dll liblzo2-2.dll libwinpthread-1.dll libsasl2-3.dll libcacard-0.dll libsnappy.dll libcairo-2.dll libcurl-4.dll libepoxy-0.dll libgdk-3-0.dll libgdk_pixbuf-2.0-0.dll libcapstone.dll libglib-2.0-0.dll libgio-2.0-0.dll libgmodule-2.0-0.dll libgnutls-30.dll libiconv-2.dll libgtk-3-0.dll libintl-8.dll libjpeg-8.dll libncursesw6.dll libnfs-14.dll libpixman-1-0.dll libpng16-16.dll libgobject-2.0-0.dll SDL2.dll SDL2_image.dll libslirp-0.dll libspice-server-1.dll libssh.dll libusb-1.0.dll libusbredirparser-1.dll zlib1.dll libzstd.dll libnspr4.dll nss3.dll libgcc_s_seh-1.dll libstdc++-6.dll libfontconfig-1.dll libfreetype-6.dll libbrotlidec.dll libidn2-0.dll libcrypto-3-x64.dll libnghttp2-14.dll libpsl-5.dll libssl-3-x64.dll libssh2-1.dll libpcre2-8-0.dll libbrotlienc.dll libgmp-10.dll libhogweed-6.dll libnettle-8.dll libp11-kit-0.dll libtasn1-6.dll libunistring-5.dll libatk-1.0-0.dll libcairo-gobject-2.dll libfribidi-0.dll libharfbuzz-0.dll libffi-8.dll libpango-1.0-0.dll libpangowin32-1.0-0.dll libavif-16.dll libjxl.dll libtiff-6.dll libwebp-7.dll libwebpdemux-2.dll libpangocairo-1.0-0.dll libgstapp-1.0-0.dll libgstreamer-1.0-0.dll liblz4.dll libopus-0.dll liborc-0.4-0.dll nssutil3.dll libplc4.dll libplds4.dll libexpat-1.dll libbrotlicommon.dll libgraphite2.dll libthai-0.dll libdav1d-7.dll libaom.dll libsharpyuv-0.dll rav1e.dll libSvtAv1Enc.dll libyuv.dll libhwy.dll libjxl_cms.dll libdeflate.dll libjbig-0.dll libLerc.dll liblzma-5.dll libpangoft2-1.0-0.dll libgstbase-1.0-0.dll libdatrie-1.dll liblcms2-2.dll /c/qemu/