Linux下使用vscode开发基于Qt的应用
使用脚本作为Tasks的输入,Tasks只作为vscode的接口,具体的编译、生成任务由脚本完成,脚本需要的参数可以通过args传入。
任务配置
这里用到的编译、生成的脚本,要在命令行先跑一下,验证无误再使用。
tasks.json如下:
{
"version": "2.0.0",
"tasks":[
{
"label": "build_project",
"type": "shell",
"command": "./build_dbg.sh",
"args": [],
"group": "build",
"problemMatcher": [
"$gcc"
]
},
{
"label": "run_project",
"type": "shell",
"command": "./build/xxx", //xxx表示生成的可执行文件
"args": [],
"group": "test",
"problemMatcher": [
"$gcc"
]
},
{
"label": "clean_project",
"type": "shell",
"command": "rm",
"args": [
"-rf",
"./build"
],
"group": "test",
"problemMatcher": [
"$gcc"
]
}
]
}
调试配置
首先确保vscode中已经完成了C++开发环境的配置,这里推荐几个好用的插件,如下图:

launch.json中:
- “program” 需要指定可执行文件,vscode提供的相对路径可以在官网查询,下面的例子使用的是工程文件的路径,即.vscode文件夹所在的路径。
- “preLaunchTask” 在启动launch之前执行tasks.json中的任务,这里把带有调试信息的build任务的标签填在这里,可以实现“一键debug”的功能。
launch.json如下:
{
"version": "0.2.0",
"configurations": [
{
"name": "debug project",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/xxx",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"MIMode": "gdb",
"miDebuggerPath": "usr/bin/gdb",
"preLaunchTask": "build_project",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
编译的脚本
build_dbg.sh,这里要注意.pro文件的路径!
#!/bin/bash
mkdir -p build
cd build
/us/lib/qt5/bin/qmake xxx.pro -spec linux-g++ CONFIG+=debug # 这里的CONFIG和.pro文件里面的作用是一样的
make -j32 # 指定编译的线程数

本文介绍了如何在Linux环境下使用Visual Studio Code(VSCode)开发Qt应用。通过设置任务配置、调试配置和使用编译脚本来实现Qt项目的构建和调试。详细讲解了tasks.json和launch.json的配置,并强调了编译脚本build_dbg.sh中.pro文件路径的重要性。
1534

被折叠的 条评论
为什么被折叠?



