0
点赞
收藏
分享

微信扫一扫

VSCode QT C++配置

伢赞 2022-05-06 阅读 141
qtvscodec++

VSCode下载【Open】插件,这样右击文件能以默认应用打开文件,这样右击打开【.ui】文件,就能在QT的【designer.exe】中打开了。

利用QT的【uic.exe】将【.ui】转换为【.h】C++格式头文件,然后带有Q_OBJECT宏的文件需要被QT的【moc.exe】MOC成CPP文件。然后gcc或g++就能编译了。

VSCode需要配置的json文件主要有【c_cpp_properties.json】、【launch.json】、【tasks.json】。

【c_cpp_properties.json】中需要配置头文件路径、编译器g++.exe路径、C标准C11、C++标准C++11。

【launch.json】中需要配置是否外置控制台、生成的EXE文件路径及名称、最后一个任务的名称。

【tasks.json】中需要配置详细的任务,如EXE加入图标、QT ui文件转换为C++文件、带Q_OBJECT宏的文件需要MOC成CPP文件、g++编译、删除图标.o文件。g++编译,要把所有的CPP和C文件都包括进去,头文件目录、库目录、库基础名称也要包含。若是CMD直接打指令操作则设置command为 ""。多个task之间使用【dependsOn】来指定顺序。

下面是一个配置样板:

【c_cpp_properties.json】

{

    "configurations": [

        {

            "name": "Win32",

            "includePath": [

                "${workspaceFolder}\\**",

                "C:\\Qt\\Qt5.14.2\\5.14.2\\mingw73_64\\include",

                "C:\\Qt\\Qt5.14.2\\5.14.2\\mingw73_64\\include\\QtWidgets",

                "C:\\Qt\\Qt5.14.2\\5.14.2\\mingw73_64\\include\\QtGui",

                "C:\\Qt\\Qt5.14.2\\5.14.2\\mingw73_64\\include\\QtCore"

            ],

            "defines": [

                "_DEBUG",

                "UNICODE",

                "_UNICODE"

            ],

            "compilerPath": "C:\\Qt\\Qt5.14.2\\Tools\\mingw730_64\\bin\\g++.exe",

            "cStandard": "c11",

            "cppStandard": "c++11",

            "intelliSenseMode": "windows-gcc-x64"

        }

    ],

    "version": 4

}

【launch.json】

{

    "version": "0.2.0",

    "configurations": [

        {

            "name": "g++.exe - 生成和调试活动文件",

            "type": "cppdbg",

            "request": "launch",

            "program": "${fileDirname}\\${workspaceFolderBasename}.exe",

            "args": [],

            "stopAtEntry": false,

            "cwd": "${fileDirname}",

            "environment": [],

            "externalConsole": true,

            "MIMode": "gdb",

            "miDebuggerPath": "C:\\Qt\\Qt5.14.2\\Tools\\mingw730_64\\bin\\gdb.exe",

            "setupCommands": [

                {

                    "description": "为 gdb 启用整齐打印",

                    "text": "-enable-pretty-printing",

                    "ignoreFailures": true

                }

            ],

            "preLaunchTask": "删除图标.o文件"

        }

    ]

}

【tasks.json】

{

    "tasks": [

        {

            "type": "cppbuild",

            "label": "删除图标.o文件",

            "command": "",

            "args": [

                "del",

                "${fileDirname}\\${fileBasenameNoExtension}.o"

            ],

            "options": {

                "cwd": "${fileDirname}"

            },

            "problemMatcher": [

                "$gcc"

            ],

            "group": {

                "kind": "build",

                "isDefault": true

            },

            "dependsOn": "C/C++: g++.exe 生成活动文件"

        },

        {

            "type": "cppbuild",

            "label": "C/C++: g++.exe 生成活动文件",

            "command": "C:\\Qt\\Qt5.14.2\\Tools\\mingw730_64\\bin\\g++.exe",

            "args": [

                "-o2", //-g为debug,-o2为release

                //"-mwindows", //不输出控制台

                "${fileDirname}\\*.cpp",

                "${fileDirname}\\${fileBasenameNoExtension}.o",

                "-o",

                "${fileDirname}\\${workspaceFolderBasename}.exe",

                "-l","gdi32",

                "-l","ws2_32",

                "-l","winmm",

                "-l","comdlg32",

                "-I","C:\\Qt\\Qt5.14.2\\5.14.2\\mingw73_64\\include",

                "-I","C:\\Qt\\Qt5.14.2\\5.14.2\\mingw73_64\\include\\QtWidgets",

                "-I","C:\\Qt\\Qt5.14.2\\5.14.2\\mingw73_64\\include\\QtGui",

                "-I","C:\\Qt\\Qt5.14.2\\5.14.2\\mingw73_64\\include\\QtCore",

                "-L","C:\\Qt\\Qt5.14.2\\5.14.2\\mingw73_64\\lib",

                "-l","Qt5Core",

                "-l","Qt5Gui",

                "-l","Qt5Widgets",

                "-l","d3d11",

            ],

            "options": {

                "cwd": "${fileDirname}"

            },

            "problemMatcher": [

                "$gcc"

            ],

            "group": {

                "kind": "build",

                "isDefault": true

            },

            "dependsOn":"Q_OBJECT moc.h 转 moc.cpp"

        },

        {

            "type": "cppbuild",

            "label": "Q_OBJECT moc.h 转 moc.cpp",

            "command": "",

            "args": [

                "moc",

                "${fileDirname}\\moc.h",

                "-o",

                "${fileDirname}\\moc.cpp"

            ],

            "options": {

                "cwd": "${fileDirname}"

            },

            "problemMatcher": [

                "$gcc"

            ],

            "group": {

                "kind": "build",

                "isDefault": true

            },

            "dependsOn": "ui.ui 转 ui.h(c++)"

        },

        {

            "type": "cppbuild",

            "label": "ui.ui 转 ui.h(c++)",

            "command": "",

            "args": [

                "uic",

                "${fileDirname}\\ui.ui",

                "-o",

                "${fileDirname}\\ui.h"

            ],

            "options": {

                "cwd": "${fileDirname}"

            },

            "problemMatcher": [

                "$gcc"

            ],

            "group": {

                "kind": "build",

                "isDefault": true

            },

            "dependsOn": "EXE加入图标"

        },

        {

            "type": "cppbuild",

            "label": "EXE加入图标",

            "command": "C:\\Qt\\Qt5.14.2\\Tools\\mingw730_64\\bin\\windres.exe",

            "args": [

                "-i",

                "${fileDirname}\\${fileBasenameNoExtension}.rc",

                "${fileDirname}\\${fileBasenameNoExtension}.o"

            ],

            "options": {

                "cwd": "${fileDirname}"

            },

            "problemMatcher": [

                "$gcc"

            ],

            "group": {

                "kind": "build",

                "isDefault": true

            },

            "dependsOn": ""

        }

    ],

    "version": "2.0.0"

}

举报

相关推荐

VScode配置C/C++环境

vscode 配置C/C++环境

VSCode配置C/C++环境

【C++】VScode配置环境

vscode配置C++环境

vscode配置c,c++开发环境

VSCode中配置C/C++环境

0 条评论