Visual Studio Code 的C++环境配置和调试

目录

前言:

1.为什么要写这篇文章?

2.为什么选择VS Code?

3.如何联系我?

简而言之,VS Code免费+快速+好用+潜力无穷;

一.安装VS Code:

二.配置C/C++环境:

步骤1:寻找MinGw编译器;

步骤2:添加环境变量:

步骤3:开始着手VS Code工作;

三.C/C++调试文件配置;


前言:

1.为什么要写这篇文章?

新学期学习C++,显然Dev Cpp编译器停止了更新,难免老旧。因此,我们需要一个新的支持C++环境的代码编辑器或IDE。

2.为什么选择VS Code?

1.Visual studio 2022很专业,但操作繁琐,难免不适合初学者,如果仅仅配置C++插件和工具,就需要17.89G;

2.Codeblocks的UI颜值偏低,不能激起我学习C++的兴趣。而且,创建C++文件时的操作思路和上学期使用的Dev Cpp逻辑不同;

3.VS Code拥有强大的插件,较低的内存占用和极快的反应速度,适合初学者;

4.VS Code支持多种背景,支持简体中文;

3.如何联系我?

wei.haoran@outlook.com

[email protected]

时间仓促,请不吝赐教;

简而言之,VS Code免费+快速+好用+潜力无穷;

学习之路道阻且长,感谢所有支持我,亦或是给予我灵感的人!


一.安装VS Code:

        1.前往VS Code下载安装:Visual Studio Code - Code Editing. Redefinedhttps://code.visualstudio.com/

二.配置C/C++环境:

1.VS Code本质上是一个代码编辑器,并不自带编译器。因此,我们需要自行下载MinGw编译器;

Codeblocks内置完整的MinGw编译器,因此我们可以通过环境变量配置的方法间接得到MinGw编译器;

2.肯定有同学会问,为什么不去MinGw官网下载?由于网络原因和其他原因,在我尝试以后,发现这种解决方案并不快速。要知道,我们的目标是学习C++语言,重点不在C++的环境配置,所以我们可以使用现成的编译器。

步骤1:寻找MinGw编译器;

 OK,复制bin文件夹的地址。

步骤2:添加环境变量:

设置-系统-关于-高级系统设置-高级-环境变量-Path-新建;

尔后,将步骤一的bin文件夹地址粘入;

步骤3:开始着手VS Code工作;

1.C++插件推荐:前5项必装,Code Runner选装;(Ctrl + Shift + X);

2.VS Code背景切换:Ctrl K + Ctrl T;推荐Monokai Dimmed;

3.Test代码:

#include<iostream>
//A class can have multiple public,private and protected label areas;
//Each marked area is valid until the next marked area begins or until the closing closing bracket of the class body is encountered.
//The default access modifier for members and classes is private;
using namespace std;
//public example;
class Line{
    public:
    int length;
    void setlength(int len);
    int showlength(void);
};
void Line::setlength(int len){
    length = len;
}
int Line::showlength(void){
    return length;
}

int main(){
    Line line1;
    int a;

    cout<<"Please enter the length that you prefer:"<<endl;
    cin>>a;
    line1.setlength(a);//normal operation;
    int result = line1.showlength();
    cout<<"The length which you have entered is "<<result<<endl;

    cout<<"Again:"<<endl;
    cin>>line1.length;//OK,length is public access modifier;
    cout<<"The length which you have entered is "<<line1.length<<endl;
    return 0;
}

 4.插件安装完成以后,选择编译(Ctrl + Shift + Build,C语言选择gcc,C++选择g++,如图所示:

步骤2的小三角安装Code Runner后会出现;

 5.Nice,完成!

三.C/C++调试文件配置;

推荐阅读:Debugging in Visual Studio Code

1.文件夹及其文件名不能出现中文名,否则报错;

2.在调试的文件夹内,新建.vscode文件夹,新建两个文件:launch.json & tasks.json;

3.launch.json:

注意:

miDebuggerPath的设置,与gdb.exe地址一致,gdb.exe在bin文件夹内;

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "C/C++", // 配置名称,将会在启动配置的下拉菜单中显示
            "type": "cppdbg", // 配置类型,这里只能为cppdbg
            "request": "launch", // 请求配置类型,可以为launch(启动)或attach(附加)
            "program": "${fileDirname}/${fileBasenameNoExtension}.exe", // 将要进行调试的程序的路径
            "args": [], // 程序调试时传递给程序的命令行参数,一般设为空即可
            "stopAtEntry": false, // 设为true时程序将暂停在程序入口处,一般设置为false
            "cwd": "${workspaceFolder}", // 调试程序时的工作目录,一般为${fileDirname}即代码所在目录
            "environment": [],
            "externalConsole": false, // 调试时是否显示控制台窗口,一般设置为true显示控制台
            "MIMode": "gdb",
            "miDebuggerPath": "C://Program Files//CodeBlocks//MinGW//bin//gdb.exe", // miDebugger的路径,注意这里要与MinGw的路径对应
            "preLaunchTask": "g++", // 调试会话开始前执行的任务,一般为编译程序,c++为g++, c为gcc
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
        },
    ]
}

 4.tasks.json

{
    "version": "2.0.0",
    "command": "g++",
    "args": [
        "-g",
        "${file}",
        "-o",
        "${fileDirname}/${fileBasenameNoExtension}.exe"
    ],
    "problemMatcher": {
        "owner": "cpp",
        "fileLocation": [
            "relative",
            "${workspaceRoot}"
        ],
        "pattern": {
            "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
            "file": 1,
            "line": 2,
            "column": 3,
            "severity": 4,
            "message": 5
        }
    },
    "group": {
        "kind": "build",
        "isDefault": true
    }
}

5.设置断点,尔后Ctrl + Shift + D调试;

 6.Nice,完成!

猜你喜欢

转载自blog.csdn.net/m0_63478913/article/details/123420984