debian下配置vscode+qt开发环境

前言

其实在qtcreator中开发qt就是最好的环境了。奈何时光编码水平不行,想借助AI的力量,只有vscode等编辑器能支持各种AI编码插件,所以产生了在vscode上部署qt开发环境的想法。

这里教程并没有完全舍弃qtcreator,是qtcreator+vscode结合使用。在编码阶段使用vscode,项目创建以及ui设计等在qtcreator上完成。

一、在qtcreator中创建项目

1.创建项目,常规步骤。

注意:创建的项目名称与创建路径名称一致,这个为后面在vscode上配置环境提供方便。

图片[1] - debian下配置vscode+qt开发环境 - 正则时光

2.创建完成后,点击Debug以及Release方式执行一下。

图片[2] - debian下配置vscode+qt开发环境 - 正则时光

3.执行完成后,获取这样的项目工作目录

图片[3] - debian下配置vscode+qt开发环境 - 正则时光

二、vscode配置

安装插件

图片[4] - debian下配置vscode+qt开发环境 - 正则时光

1.导入项目

图片[5] - debian下配置vscode+qt开发环境 - 正则时光

2.编辑c/c++设置

编辑配置,ctrl+shift+p,选择编辑配置(UI)。

图片[6] - debian下配置vscode+qt开发环境 - 正则时光

配置如下,点击左侧c_cpp_properties.json文件,生成c_cpp_properties.json配置文件。

图片[7] - debian下配置vscode+qt开发环境 - 正则时光
{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "/opt/Qt5.14.2/5.14.2/gcc_64/include/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/g++",
            "cStandard": "c17",
            "cppStandard": "gnu++17",
            "intelliSenseMode": "linux-gcc-x64"
        }
    ],
    "version": 4
}

3.配置tasks.json

选择终端-运行任务-使用模板创建tasks.json文件-others。获得一个模板,在这个模板上来修改。

图片[8] - debian下配置vscode+qt开发环境 - 正则时光
图片[9] - debian下配置vscode+qt开发环境 - 正则时光
图片[10] - debian下配置vscode+qt开发环境 - 正则时光

照着qtcreator中的debug和release的build+run的配置来修改tasks.json文件。可照着下面的示例根据自己的实际情况来修改。

图片[11] - debian下配置vscode+qt开发环境 - 正则时光
{
    "version": "2.0.0",
    "tasks": [
      {
        "label": "qt-qmake-debug",
        "type": "shell",
        "command": "qmake",
        "args": [
          "${workspaceFolder}/${workspaceFolderBasename}/${workspaceFolderBasename}.pro",
          "-spec",
          "linux-g++",
          "CONFIG+=debug",
          "CONFIG+=qml_debug"
        ],
        "options": {
          "cwd": "${workspaceFolder}/build-${workspaceFolderBasename}-Desktop_Qt_5_14_2_GCC_64bit-Debug"
        },
        "problemMatcher": [
          "$gcc"
        ]
      },
      {
        "label": "qt-make-debug",
        "type": "shell",
        "command": "make",
        "args": [
          "-j8"
        ],
        "options": {
          "cwd": "${workspaceFolder}/build-${workspaceFolderBasename}-Desktop_Qt_5_14_2_GCC_64bit-Debug"
        },
        "problemMatcher": [
          "$gcc"
        ],
        "dependsOn": ["qt-qmake-debug"]
      },
      {
        "label": "qt-make-clean-debug",
        "type": "shell",
        "command": "make",
        "args": [
          "clean",
          "-j8"
        ],
        "options": {
          "cwd": "${workspaceFolder}/build-${workspaceFolderBasename}-Desktop_Qt_5_14_2_GCC_64bit-Debug"
        },
        "problemMatcher": [
          "$gcc"
        ],
        "dependsOn": ["qt-make-debug"]
      },
      {
        "label": "qt-run-debug",
        "type": "process",
        "command": "${workspaceFolder}/build-${workspaceFolderBasename}-Desktop_Qt_5_14_2_GCC_64bit-Debug/${workspaceFolderBasename}",
        "options": {
          "cwd": "${workspaceFolder}/build-${workspaceFolderBasename}-Desktop_Qt_5_14_2_GCC_64bit-Debug"
        },
        "dependsOn": ["qt-make-clean-debug"]
      },
      {
        "label": "qt-qmake-release",
        "type": "shell",
        "command": "qmake",
        "args": [
          "${workspaceFolder}/${workspaceFolderBasename}/${workspaceFolderBasename}.pro",
          "-spec",
          "linux-g++",
          "CONFIG+=qtquickcompiler"
        ],
        "options": {
          "cwd": "${workspaceFolder}/build-${workspaceFolderBasename}-Desktop_Qt_5_14_2_GCC_64bit-Release"
        },
        "problemMatcher": [
          "$gcc"
        ]
      },
      {
        "label": "qt-make-release",
        "type": "shell",
        "command": "make",
        "args": [
          "-j8"
        ],
        "options": {
          "cwd": "${workspaceFolder}/build-${workspaceFolderBasename}-Desktop_Qt_5_14_2_GCC_64bit-Release"
        },
        "problemMatcher": [
          "$gcc"
        ],
        "dependsOn": ["qt-qmake-release"]
      },
      {
        "label": "qt-make-clean-release",
        "type": "shell",
        "command": "make",
        "args": [
          "clean",
          "-j8"
        ],
        "options": {
          "cwd": "${workspaceFolder}/build-${workspaceFolderBasename}-Desktop_Qt_5_14_2_GCC_64bit-Release"
        },
        "problemMatcher": [
          "$gcc"
        ],
        "dependsOn": ["qt-make-release"]
      },
      {
        "label": "qt-run-release",
        "type": "process",
        "command": "${workspaceFolder}/build-${workspaceFolderBasename}-Desktop_Qt_5_14_2_GCC_64bit-Release/${workspaceFolderBasename}",
        "options": {
          "cwd": "${workspaceFolder}/build-${workspaceFolderBasename}-Desktop_Qt_5_14_2_GCC_64bit-Release"
        },
        "dependsOn": ["qt-make-clean-release"]
      }
    ]
  }
  

4.vscode配置断点调试

侧边栏选择调试,点击创建launch.json文件,点击添加配置,选择c/c++:(gdb)启动作为模板。

图片[12] - debian下配置vscode+qt开发环境 - 正则时光

根据模板修改launch.json。参考文件如下:

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "debug", // 调试配置名称
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/build-${workspaceFolderBasename}-Desktop_Qt_5_14_2_GCC_64bit-Debug/${workspaceFolderBasename}", // 调试程序的路径
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}", // 工作目录
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "/usr/bin/gdb", // gdb 路径
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "qt-make-debug", // 编译程序的前置任务名称
        }
    ]
}

三、运行及调试效果

1.运行

终端-运行任务。可以看到我们在tasks.json中添加的任务,选择qt-run-debug,即可运行,因为使用了depens on参数,会自动执行前面的qmake/make/clean等任务的。

图片[13] - debian下配置vscode+qt开发环境 - 正则时光
图片[14] - debian下配置vscode+qt开发环境 - 正则时光

2.调试

这里点击侧边栏的调试就能看到我们添加的调试launch.json文档的名称了。打个断点,点击绿色按钮测试一下。

图片[15] - debian下配置vscode+qt开发环境 - 正则时光
© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享