vscode-xiaobing1016C++环境搭建

vscode-xiaobing1016-C++环境搭建

0、首先搭建好gcc和cmake环境,也即要设置好他们的环境变量到自己主机之中
1、vscode相关插件安装

单文件运行

编写好单文件程序之后,按F5键就会自动生成.vscode和可执行程序

image

随后通过g++命令编译即可,单文件g++很简单,略

单文件调试

首先配置launch.json  和  tasks.json
下面  launch.json  和  tasks.json都是仅适用于单文件调试
launch.json
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++.exe - 生成和调试活动文件",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "D:\\software\\mingw\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "将反汇编风格设置为 Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++.exe 生成活动文件"
        }
    ]
}
tasks.json
{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe 生成活动文件",
            "command": "D:\\software\\mingw\\bin\\g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        }
    ],
    "version": "2.0.0"
}
配置好  launch.json  和  tasks.json都就可以按F5进行调试啦

多文件编译运行

注意:多文件排列方式和上一博客不同

image

编译运行简单示例

image

多文件调试

第1步:指定调试程序的名字,需要一致
生成的调试程序名字如何配置,见下图 

image

第2步:这种多文件排列的方式,在调试时launch.json尾部preLaunchTask的值要注释掉

image

第3步:生成可执行程序时务必加上  -g

image

第4步:F5即可开始调试 (注意:貌似不能有tasks.json才能正常调试)
多文件调试完毕

Cmake方式进行编译调试

准备工作开始

CMakeLists.txt内容如下:
CMakeLists.txt
project(MYSWAP)

add_executable(app main.cpp swap.cpp)
保存CMakeLists.txt就会自动生成build文件夹
进入build文件夹执行
cmake ..
mingw32-make.exe
即可生成调试程序

image

想要调试必须把可执行程序与launch.json里面的路径关联关系配置好
launch.json里面尾部的preLaunchTask也必须注释
必须要做,不然肯定无法调试

image

至此,准备工作完毕
按F5按键即可开始调试

很可能会遇到的错误解决:

若手动创建build或者build里面没有任何内容的情况下:
极有可能会出现下面这种错误:
竟然开始调用微软的编译器

image

解决方案
 cmake -G "MinGW Makefiles" ..

image
image

中级进阶

单独的launch.json由于必须屏蔽尾部的preLaunchTask
这样很可能会产生无法解决可执行程序与程序文件之间的时间戳校验问题
由此引出,一旦稍微改动代码,断点调试依然是调试原来的可执行程序
并不是最新生成的程序,还可能会导致无法命中断点

下面通过配置tasks.json解决
tasks.json
{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe 生成活动文件",
            "command": "D:\\software\\mingw\\bin\\g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "main.cpp",
                "swap.cpp",
                //"main.cpp",
                "-o",
                "${fileDirname}/out1.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        }
    ],
    "version": "2.0.0"
}
launch.json
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++.exe - 生成和调试活动文件",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/out1.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "D:\\software\\mingw\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "将反汇编风格设置为 Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++.exe 生成活动文件"
        }
    ]
}

image

按下F5就会生成out1.exe,实际上没有通过cmake编译,
而是通过配置两个json文件
实现了半自动化的编译,也解决了先前提出的时间戳校验问题
不过貌似不好用,没有上一个苏大丙的教程好用

image

高级进阶

task.json和launch.json结合cmake

image

task.json
{
    "version": "2.0.0",
    "options": {
        "cwd": "${workspaceFolder}/build/"
    },

    "tasks": [
        {
            "label": "cmake",
            "type": "shell",
            "command": "cmake",
            "args": [
                ".."
            ]
        },
        {
            "label": "make",
            "group":{
                "kind":"build",
                "isDefault":true
            },
            "command": "mingw32-make.exe",
            "args":[
            ]
        },
        {
            "label":"Build my project",
            "dependsOn":[
                "cmake",
                "make"
            ]
        }
    ]
}

launch.json
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++.exe - 生成和调试活动文件",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/build/out1.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "D:\\software\\mingw\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "将反汇编风格设置为 Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "Build my project"
        }
    ]
}
posted @ 2022-11-02 18:48  mnst  阅读(40)  评论(0)    收藏  举报