Linux下使用vscode开发基于Qt的应用

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

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 # 指定编译的线程数
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值