如何从Qt Creator获取.exe输出?Mac环境生成Windows可运行.exe方法
Hey there, let's tackle both of your questions clearly—since the second one is a cross-compiling scenario (Mac to Windows), it's a bit more involved, but totally doable.
1. Getting a .exe from Qt Creator (Windows Native)
If you're working directly on a Windows machine with Qt Creator, here's the straightforward process:
- First, make sure you have the Windows version of Qt installed (either with MinGW or MSVC compiler—MinGW is easier for quick distribution).
- Open your project, then in the bottom-left corner of Qt Creator, switch to the Release build profile (skip Debug mode—those exes rely on debug libraries and won't run on non-development machines).
- Click the build button (the hammer icon) to compile your project. Once done, your raw .exe will be in the
releasesubfolder of your project directory (for MSVC, it might be inx64/Releaseor similar, depending on your architecture). - This raw exe won't run on other Windows PCs yet—it needs Qt's dependency libraries. Fix this with the
windeployqttool:- Open the Qt Command Prompt matching your compiler (e.g., "MinGW 64-bit Prompt")
- Navigate to the folder where your .exe is stored
- Run:
windeployqt your_app_name.exe
- This tool will automatically copy all required DLLs, Qt modules, and resource files into the same folder. Now you can zip up the entire folder and share it—any Windows PC can run it.
2. Cross-Compiling a Windows .exe from Qt Creator on Mac
Generating a Windows-compatible exe from a Mac requires setting up a cross-compilation toolchain. Here's the step-by-step:
- Step 1: Install the cross-compiler
Use Homebrew (if you don't have it, install it first) to get the MinGW-w64 toolchain for Windows targets. Open Terminal and run:brew install mingw-w64 - Step 2: Configure a Windows Kit in Qt Creator
Qt Creator needs a dedicated kit to cross-compile for Windows:- Go to
Tools > Options > Kits - Click
Addto create a new kit, name it something like "Windows 64-bit (MinGW Cross)" - Under the
Compilertab:- For C compiler, select
x86_64-w64-mingw32-gcc(it should show up after installing mingw-w64) - For C++ compiler, select
x86_64-w64-mingw32-g++
- For C compiler, select
- Under the
Qt Versiontab:- You need a Windows-compatible Qt library (not the Mac one!). Open the Qt Maintenance Tool, and add a Qt version like
Qt 5.15.2 > MinGW 8.1.0 64-bit(match the MinGW version from your cross-compiler if possible) - Browse to the
qmakeexecutable of this Windows Qt installation (e.g.,~/Qt/5.15.2/mingw81_64/bin/qmake) and add it to Qt Creator
- You need a Windows-compatible Qt library (not the Mac one!). Open the Qt Maintenance Tool, and add a Qt version like
- Save the kit—you're now ready to cross-compile.
- Go to
- Step 3: Build your project for Windows
- Open your sample project in Qt Creator, then switch to your new Windows kit in the bottom-left corner, and select Release mode
- Click the build button. Once finished, your Windows .exe will be in the build directory (look for a folder named something like
build-YourProject-Windows-Release)
- Step 4: Package dependencies for distribution
You can't runwindeployqtnatively on Mac for Windows exes easily, so two reliable options:- Use a Windows machine: Copy the .exe to a Windows PC, open the Qt Command Prompt there, run
windeployqt your_app.exeto gather all dependencies. - Use cross-compiled windeployqt on Mac: Locate the
windeployqtfrom your Windows Qt installation (e.g.,~/Qt/5.15.2/mingw81_64/bin/windeployqt), then run:
This will copy most Qt dependencies to a~/Qt/5.15.2/mingw81_64/bin/windeployqt --dir ./deploy your_app.exedeployfolder. You'll also need to add a few system DLLs from your mingw-w64 installation, likelibgcc_s_seh-1.dll,libstdc++-6.dll, andlibwinpthread-1.dll—find them in/usr/local/opt/mingw-w64/x86_64-w64-mingw32/binand copy them into the deploy folder.
- Use a Windows machine: Copy the .exe to a Windows PC, open the Qt Command Prompt there, run
- Zip up the deploy folder with your exe, and it'll run on any Windows PC.
Quick Notes to Avoid Headaches
- Always use Release mode for distributable exes—Debug builds are for development only.
- Make sure your Qt version matches the MinGW cross-compiler version (e.g., MinGW 8.1 with Qt built for MinGW 8.1) to avoid linker errors.
- If your project uses third-party libraries, you'll need to cross-compile those for Windows too, or use pre-built Windows versions that match your toolchain.
内容的提问来源于stack exchange,提问作者Mahdiyar
相关产品推荐
相关产品推荐

