[qt]工具配置

【参考】
PART I c++环境配置
参考资料:
Visual Studio Code 如何编写运行 C、C++ 程序?
Win10+VsCode的C/CPP编译环境搭建
I 工具准备

工具列表下载地址
VS codehttps://code.visualstudio.com
Mingw_w64http://www.mingw-w64.org/doku.php/download
LLVMhttp://releases.llvm.org/download.html

II 安装VSCode的相关插件,排序按照字母序

插件列表功能
Bracket Pair Colorizer彩色括号便于匹配
C/C++调试与代码格式化
C/C++ Clang Command Adapter智能感知与静态分析
Chinese (Simplified)简中语言包
Clang-Format自定义代码格式化
Code Runner方便运行代码
Code Spell Checker单词拼写检测
Include Autocomplete头文件补全
One Dark ProDark♂的主题
vscode-icons文件图标美化

III 环境配置
建立工作区目录

工作区目录
sgWSpace
—>.vscode
—>settings.json
tasks.json
laungh.json
compile_flags.txt
singleWorkSpace.code-workspace
build
—>生成的可执行文件
src
—>源代码

VSCode编辑器及插件设置
全局设置 settings.json
VScode左下角设置->文本编辑器->在settings.json中编辑
在这里插入图片描述

{
    "editor.minimap.enabled": true,
    "editor.renderWhitespace": "none",
    "editor.renderControlCharacters": false,
    "editor.formatOnType": false,
    "editor.snippetSuggestions": "top",
    "editor.tabSize": 2,
    "editor.useTabStops": false,
    "files.trimFinalNewlines": true,
    "files.insertFinalNewline": true,
    "files.trimTrailingWhitespace": true,
    "files.defaultLanguage": "cpp",
    "workbench.colorTheme": "One Dark Pro",
    "breadcrumbs.enabled": false,
    "workbench.iconTheme": "vscode-icons",
    "explorer.confirmDelete": false,
    "terminal.explorerKind": "integrated",
    "C_Cpp.clang_format_sortIncludes": true,
    "C_Cpp.errorSquiggles": "Disabled",		//因使用clang的代码感知,故禁用c_cpp插件自带的
    "C_Cpp.autocomplete": "Disabled",		//同上
    "C_Cpp.suggestSnippets": false,
    "C_Cpp.intelliSenseEngine": "Tag Parser",
    "C_Cpp.updateChannel": "Insiders",
    "[cpp]": {
        "editor.defaultFormatter": "xaver.clang-format"
    },
    "clang.cflags": [						//为动态感知指定编译参数,否则会出现问题
        "--target=x86_64-w64-mingw",
        "-std=c11",
        "-Wall"
    ],
    "clang.cxxflags": [						//同上
        "--target=x86_64-w64-mingw",
        "-std=c++17",
        "-Wall"
    ],
    "clang.completion.enable":true//开启后高效,据说可能导致卡顿,没有感觉到
    "clang-format.executable": "clang-format",
    "clang-format.style": "Google"
}

如注释,其他项编辑时鼠标悬停均有较为易懂的说明
大部分出自本人编程习惯,修改随意

工作区设置 settings.json

{
    "editor.formatOnType": true,
    "editor.suggest.snippetsPreventQuickSuggestions": false,
    "editor.acceptSuggestionOnEnter": "off",
    "code-runner.runInTerminal": true,
    "code-runner.executorMap": {	//设定code runner的启动命令
        "c": "cd $dir && clang '$fileName' -o '工作区路径\\build\\$fileNameWithoutExt.exe' -Wall -g -O2 -static-libgcc --target=x86_64-w64-mingw -std=c11 && &'工作区路径\\build\\$fileNameWithoutExt'",
        "cpp": "cd $dir && clang++ '$fileName' -o '工作区路径\\build\\$fileNameWithoutExt.exe' -Wall -g -O2 -static-libgcc --target=x86_64-w64-mingw -std=c++17 && &'工作区路径\\build\\$fileNameWithoutExt'"
    },
    "code-runner.saveFileBeforeRun": true,
    "code-runner.preserveFocus": false,
    "code-runner.clearPreviousOutput": true,
    "code-runner.ignoreSelection": true,
    "files.exclude": {
        ".vscode": true,
        "build": true
    }
}

同样有悬停说明
其中工作区路径 指的是工作区的完整路径
考虑工作区一般不动,就用这种暴力的方法了

(编译)任务 tasks.json

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "Compile",
      "command": "clang++",
      "args": [
        "-g",
        "-Wall",
        "-static-libgcc",
        "--target=x86_64-w64-mingw",
        "-std=c++17",
        "-fcolor-diagnostics",
        "${file}",
        "-o",
        "工作区路径\\build\\${fileBasenameNoExtension}.exe"
      ],
      "type": "shell",
      "group": {
        "kind": "build",
        "isDefault": true
      },
      "presentation": {
        "echo": false,
        "reveal": "always",
        "focus": false,
        "panel": "dedicated"
      }
    }
  ]
}

tasks类似脚本
此处为执行编译操作
完全照搬参考文章中的配置

(调试)运行 launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "工作区路径\\build\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "internalConsoleOptions": "neverOpen",
            "MIMode": "gdb",
            "miDebuggerPath": "gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": false
                }
            ],
            "preLaunchTask": "Compile"
        }
    ]
}

调试运行的配置
完全照搬参考文章中的配置

clang参数 compile_flags.txt

-Wall
--target=x86_64-w64-mingw
-std=c++17

作用与clang相关
具体见参考文章

其他

gcc -v -E -x c++ -

这个命令可以获取mingw_w64的头文件目录

Part Ⅱ - C/C++&Qt多文件编程环境配置

1. 安装Qt后配置环境变量

.\Qt\5.13.0\mingw73_64\bin
.\Qt\Tools\mingw730_64\bin

注意前一个目录应当置于

.\mingw-w64\mingw64\bin

之前,否则会导致调试时找不到qt库的问题

2.环境设置
建立工作区目录
首先在qt中新建工程(当然也可以自行编写pro文件)
选择编译器时选择Mingw_w64,其它的不勾选
随后调整目录结构,如下

工作区目录
工程名
—>build.cmd
工程名.pro
工程名.pro.user
.vscode
—>settings.json
tasks.json
laungh.json
工程名.code-workspace
Build
—>构建输出目录
include
—>头文件
src
—>源代码
ui
—>界面文件
resource
—>资源文件

其中斜体 的内容需要手动修改或创建,以下详细说明

QtCreator设置
工具–>选项–>构建和运行–>概要–>Default build directory
该项为指定输出目录,将其设为./Build
设置此项目的在于指定输出目录,默认的输出目录对我来说不太友好

工程名.pro
向其中添加(若存在则修改)

CONFIG(release):DESTDIR = ./
CONFIG(debug, debug|release):DESTDIR = ./
MOC_DIR = ./temp
OBJECTS_DIR = ./temp
TARGET = ./工程名

前两项的目的暂时还没有搞清
后三项目的也在于指定构建过程中的一些目录,让结构美观一些

工程名.pro.user
因为QtCreator的配置因为一些问题而不同步,所以要手动更新这个文件
配置完前两项后(在QtCreator中),退出QtCreator
再用QtCreator打开.pro文件,此时会弹出编译器选择,随后会自动建立这个文件
配置完这三步后已经可以使用QtCreator进行编程了

.vscode下的内容
未提到的部分同单文件时,不再赘述

tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Debug",
            "type": "shell",
            "command": "cmd",
            "args":[
                "/c",
                "${workspaceRoot}/build.cmd",
                "debug"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "label": "clear",
            "type": "shell",
            "command": "cmd",
            "args":[
                "/c",
                "${workspaceRoot}/clear.cmd",
                "debug"
            ],
            "group": "build"
        }
    ]
}

新增了清理任务,用于清理构建的内容
settings.json
修改以下设置

 "files.exclude": {
        "**/.DS_Store": true,
        "**/.git": true,
        "**/.hg": true,
        "**/.svn": true,
        "**/.vscode": true,
        "**/CVS": true,
        "**/Build": true,
        "**/build.cmd": true,
        "**/clear.cmd": true,
        "**/CMakeLists.txt": true,
        "**/*.pro": true,
        "**/resource": true,
        "**/ui": true,
        "**/*.user": true
    }
 "code-runner.executorMap": {
    "cpp": "cd $workspaceRoot\\Build && $workspaceRoot\\build.cmd && ./debug/工程名.exe",
}

前者因为构建qt时会整出很多文件,不想看到所以就处理掉吧
后者是使用runcode插件来构建工程并运行,其实runcode插件也不是很需要了

launch.json
修改执行文件路径即可
按照上述配置,其在.\Build\debug\ 下

settings.json(全局)
并不是简单地修改

clang.cxxflags": [
    "--target=x86_64-w64-mingw",
    "-std=c++17",
    "-Wall",
    "-I<你的工程目录>",
    "-I<Qt目录下你所调用库的所在路径>"
    ......
]

为了使智能感知生效,此处要对编译参数进行修改,添加include文件列表
调用多了的话实在麻烦,而且位于全局设置中会拖慢单文件环境的速度
但是实在想不到更好的解决方法了(或许可以换个插件?)
注意 -I 后不用空格直接跟着路径

clear.cmd

@echo off
title qmake clear prompt

del  工程目录\Build\'*'   /s /q
rd 工程目录\Build   /s /q
md 工程目录\Build

用于清空Build文件夹
不太会用cmd,百度后拼凑的方法(或许后两句不需要,删了再建?)

build.cmd

@echo off
title qmake build prompt

qmake 工程目录\工程名.pro -o 工程目录\Build -spec win32-g++ "CONFIG+=debug" "CONFIG+=qml_debug"
cd 工程目录\Build
mingw32-make.exe -f 工程目录/Build/Makefile.Debug qmake_all
mingw32-make.exe -j12

move 工程名.exe .\debug
cd debug
windeployqt.exe TSSW_frame.exe

cmd不太会用,就干脆都写全路径了
构建可执行程序并将其放到debug文件夹里,然后为其拷贝依赖库

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值