VScode安装与配置

VScode安装与配置

抄袭https://zhuanlan.zhihu.com/p/87864677

1、下载:

  https://code.visualstudio.com/(官网直接下载)。

2、安装:

  一直点确定、下一步就可以完成安装。

3、配置:

  3.1、中文化:

    打开vscode之后看到的是英文界面,可以在扩展的地方找到一个中文扩展包,如下图:

              

 

 

  然后重启即可。

  3.2、 编译器:

    VSCode只是一款文本编辑器,不仅需要安装对应编程语言的扩展,还需要安装相应的编译器或者解释器,这里用的是MinGW

    下载网站:https://sourceforge.net/projects/mingw-w64/files/mingw-w64/

    我这里选择的是:x86_64-win32-she,下载后直接解压就可以用了,记住文件地址,然后配置一下环境变量,如下:

    

 

 

     

 

 

     

    最后,打开cmd,输入gcc -v验证是否成功即可

    

 

 

  3.3、安装C/C++扩展及配置:

    在扩展里面搜索C/C++       

    

 

 

     安装即可,然后创建一个文件夹,自己自定义命名,如下是我的:

    

 

 

     然后通过  文件->打开文件夹    找到上面新建的文件夹。创建一个hello.cpp的文件,编辑如下内容:

#include<iostream>
#include <stdio.h>

using namespace std;

int main(){
    cout<<"Hello World!"<<endl;
    system("pause"); 
    return 0;
}

    按快捷键Ctrl+Shift+P调出命令面板,输入C/C++,选择“Edit Configurations(UI)”进入配置。这里配置两个选项: - 编译器路径:C:/x86_64-8.1.0-release-win32-seh-rt_v6-rev0/mingw64/bin/g++.exe,跳出的页面按如下配置:

    

    左侧有c_cpp_properties.json文件,按如下配置(一般默认配置即为正确配置):

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "C:/x86_64-8.1.0-release-win32-seh-rt_v6-rev0/mingw64/bin/g++.exe",
            "cStandard": "c17",
            "cppStandard": "gnu++14",
            "intelliSenseMode": "gcc-x64"
        }
    ],
    "version": 4
}

  3.4、配置任务创建:

    按快捷键Ctrl+Shift+P调出命令面板,输入tasks,选择“Tasks:Configure Default Build Task”;再选择“C/C++: g++.exe build active file”。

    此时会出现一个名为tasks.json的配置文件;按如下配置:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558 
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "g++.exe build active file",//任务的名字,就是刚才在命令面板中选择的时候所看到的,可以自己设置
            "command": "C:/x86_64-8.1.0-release-win32-seh-rt_v6-rev0/mingw64/bin/g++.exe",
            "args": [//编译时候的参数
                "-g",//添加gdb调试选项
                "${file}",
                "-o",//指定生成可执行文件的名称
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "C:/x86_64-8.1.0-release-win32-seh-rt_v6-rev0/mingw64/bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true//表示快捷键Ctrl+Shift+B可以运行该任务
            }
        }
    ]
}

  3.5、配置调试设置:

    点击菜单栏的运行->启动调试;会跳出一个菜单:

    

 

     选择C++(GDB/LLDB);紧接着会产生一个launch.json的文件,如下配置:

    

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [

        {
            "name": "(gdb) Launch",
            "preLaunchTask": "g++.exe build active file",//调试前执行的任务,就是之前配置的tasks.json中的label字段
            "type": "cppdbg",//配置类型,只能为cppdbg
            "request": "launch",//请求配置类型,可以为launch(启动)或attach(附加)
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",//调试程序的路径名称
            "args": [],//调试传递参数
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,//true显示外置的控制台窗口,false显示内置终端
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\x86_64-8.1.0-release-win32-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

    然后再开始调试便没有了问题。

#include<iostream>
#include <stdio.h>

using namespace std;

int main(){
    cout<<"Hello World!"<<endl;
    system("pause"); 
    return 0;
}

    这里的system("pause");代码可以在结果展示之后不会立马退出,结果如下:

    

 

posted @ 2023-04-13 15:55  Carllll  阅读(448)  评论(0编辑  收藏  举报