Qt技术问题:为特定子项目更换qmake版本
Switching qmake Version for Specific Qt Sub-Projects (Subdirs Template)
Alright, let's tackle how to switch the qmake version for a specific sub-project in your Qt subdirs setup. Here's a step-by-step breakdown based on common scenarios:
1. Locate your target qmake executable
First, you need the full path to the qmake binary from the Qt version you want to use for your specific sub-project. For example:
- On Linux/macOS:
/opt/Qt/5.15.2/gcc_64/bin/qmake - On Windows:
C:\Qt\5.15.2\mingw81_64\bin\qmake.exe
This path varies depending on your Qt installation directory and the compiler variant you're targeting.
2. Using Qt Creator (GUI Approach)
If you're using Qt Creator for development, this is the most straightforward method:
- Open your project and switch to the Projects mode (left sidebar icon that looks like a folder with a wrench).
- Select the specific sub-project (e.g.,
sub_project_2) from the list on the left. - In the Build & Run tab, click Add under Kits to create a new kit, or modify an existing kit:
- In the kit settings, set the Qt version to your target Qt release (which will automatically map to the correct qmake).
- Assign this custom kit to your target sub-project. All other sub-projects can remain on their original kits.
- Don't forget to clean the build directory for the sub-project before rebuilding to avoid cached artifacts.
3. Manual Command-Line Build
If you prefer building via terminal/command prompt:
- Navigate directly to the directory of your target sub-project:
cd project/sub_project_2 - Run the target qmake executable against the sub-project's
.profile:# Linux/macOS example /opt/Qt/5.15.2/gcc_64/bin/qmake sub_project_2.pro:: Windows example (Command Prompt) C:\Qt\5.15.2\mingw81_64\bin\qmake.exe sub_project_2.pro - Compile as usual with
make,mingw32-make, ornmakedepending on your compiler.
4. Integrate into the Main Subdirs Project
If you want the main project.pro to handle building the specific sub-project with the alternate qmake version automatically:
- Open your main
project.profile and modify theSUBDIRSsection to explicitly specify the qmake path for the target sub-project:
This tells the main project's build system to use the specified qmake when processingSUBDIRS += sub_project_1 sub_project_2 sub_project_3 # Override qmake for sub_project_2 sub_project_2.subdir = sub_project_2 sub_project_2.qmake = /opt/Qt/5.15.2/gcc_64/bin/qmakesub_project_2, while using the default qmake for other sub-projects.
Critical Notes
- ABI Compatibility: If your sub-projects depend on each other (e.g.,
sub_project_1links againstsub_project_2), ensure their Qt versions are compatible. Mixing Qt versions with different ABIs will cause linker errors. - Clean Builds: Always delete the sub-project's build directory when switching qmake versions to avoid leftover files from the previous build configuration.
- Common Pri Files: Verify that your
common/common.priuses syntax and Qt modules supported by the target qmake version. Older Qt versions may not support newer module syntax or features.
内容的提问来源于stack exchange,提问作者C. Haas




