To set up and compile Qt projects in Visual Studio Code (VSCode), follow these steps. This guide uses QMake as the build system and assumes you have Qt and VSCode installed.
- Install Required Tools
Install Qt (e.g., Qt 5.15 or later) and ensure the required compiler (e.g., MinGW or MSVC) is installed.
Install VSCode and the following extensions: C/C++ by Microsoft. Qt Tools or Qt Extension Pack for better integration.
- Configure Environment Variables
Add the following paths to your system's environment variables: Qt/bin (e.g., C:/Qt/5.15.2/mingw81_64/bin). Compiler path (e.g., C:/Qt/Tools/mingw810_64/bin).
- Open the Project in VSCode
Create a Qt project using Qt Creator or manually create a .pro file.
Open the project folder in VSCode.
- Configure VSCode
a. Configure C/C++ Plugin
Edit .vscode/c_cpp_properties.json:
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"C:/Qt/5.15.2/mingw81_64/include/**"
],
"compilerPath": "C:/Qt/Tools/mingw810_64/bin/g++.exe",
"intelliSenseMode": "windows-gcc-x64",
"cStandard": "c17",
"cppStandard": "c++17"
}
],
"version": 4
}
Create .vscode/tasks.json to define build tasks:
{
"version": "2.0.0",
"tasks": [
{
"label": "qmake-debug",
"type": "shell",
"command": "qmake",
"args": [
"../${workspaceFolderBasename}.pro",
"-spec",
"win32-g++",
"\"CONFIG+=debug\""
],
"options": {
"cwd": "${workspaceFolder}/build"
}
},
{
"label": "make-debug",
"type": "shell",
"command": "mingw32-make",
"args": [
"-j8"
],
"options": {
"cwd": "${workspaceFolder}/build"
},
"dependsOn": [
"qmake-debug"
]
}
]
}
c. Configure Debugging
Create .vscode/launch.json for debugging:
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/debug/${workspaceFolderBasename}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:/Qt/Tools/mingw810_64/bin/gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "make-debug"
}
]
}
- Build and Run
Use Ctrl+Shift+B to build the project.
For debugging, press F5 to start debugging with breakpoints.
Tips
Ensure all paths in configuration files match your local setup.
Use Ctrl+Shift+P > Run Task to execute specific tasks like qmake-debug.
This setup allows you to efficiently compile and debug Qt projects directly in VSCode
回头有空,整理出来一份 mac 版的