C/C++ Environment Configuration for Playing VS Code

PS: I am a rookie, it took a lot of time to sort out and step on the pit trial and error. If this article is useful to you, please leave a free like, give someone a rose, leave a lingering fragrance in your hand, and step on the pit with code words It's not easy, hope Sanlian support

Previous: Playing with VS Code Downloads

Msys2

MinGW works too, but I'm using msys2

Download Msys2

Address: MSYS2

 

 Install Msy2

 Brainless agree till the end

 

 Complete the mingw toolchain installation

open page

 Enter the command, update (the installation file is the latest and you can go directly to the next step)

pacman -Syu

After the update is complete, the terminal will be forced to close, and run "MSYS2 MSYS" from the start menu

Update the rest of the base packages with

pacman -Syu

 Enter the following command to complete the mingw toolchain installation

pacman -S --needed base-devel mingw-w64-x86_64-toolchain

 Check if the compiler configuration was successful

After the compiler installation is complete, win+R enter the following two commands in the cmd window to check whether the compiler configuration is successful

gcc -v

gdb -v

C/C++ environment configuration 

Because the C++ environment is configured, the C language can also be used directly, so I will configure C++ directly.

Small white version

After I stepped on the pit test several times and deleted it, it is available for personal testing (a handful of bitter tears)

There is also an advanced version in the back, which needs to be changed the same, but with many more parameters and notes, if you are interested, you can take a look

You can get started quickly by changing a few places. The ones to be changed have been marked, and you can change them according to your own installation path

 First create a folder called .vscode (put the configuration file, don’t put the source code in it, I don’t know what will happen if you put it in)

Then create 4 json files with the same name in it, put my code in it, and then modify a few marked places

launch.json
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "弹窗输出",
            "type": "cppdbg",
            "request": "launch",
            "program": "C:\\windows\\System32\\cmd.exe",
            "args": [
                "/C",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "&",
                "pause"
            ],
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "F:\\vscode\\MSY\\MSY2\\ucrt64\\bin\\gdb.exe", //改成自己的路径的gdb.exe
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "将反汇编风格设置为 Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "mytask"
        },
        {
            "name": "终端输出",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "F:\\vscode\\MSY\\MSY2\\ucrt64\\bin\\gdb.exe", //改成自己的路径的gdb.exe
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "将反汇编风格设置为 Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "myrun"
        }
    ]
}
tasks.json
{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "mytask",
            "command": "F:\\vscode\\MSY\\MSY2\\ucrt64\\bin\\g++.exe", //改成自己的路径
            "args": [
                "-fexec-charset=gbk",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
            ],
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build",
            "detail": "弹窗输出"
        },
        {
            "type": "shell",
            "label": "myrun",
            "command": "F:\\vscode\\MSY\\MSY2\\ucrt64\\bin\\g++.exe", //改成自己的路径
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {},
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build",
            "detail": "终端内输出"
        },
        {
            "type": "cppbuild",
            "label": "run",
            "command": "F:\\vscode\\MSY\\MSY2\\ucrt64\\bin\\g++.exe", //改成自己的路径
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {},
            "problemMatcher": [
                "$gcc"
            ],
            "group": "test",
            "detail": "终端内输出"
        }
    ]
}
c_cpp_properties.json
{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "F:\\vscode\\MSY\\MSY2\\ucrt64\\bin\\g++.exe", //改成自己的路径
            "cStandard": "gnu17",
            "cppStandard": "gnu++17",
            "intelliSenseMode": "${default}"
        }
    ],
    "version": 4
}
settings.json

This is not very important, it seems that no code can be run

{
    "files.associations": { //配置语言的文件关联,优先级高于已安装语言的默认关联。
        "ostream": "cpp"
    },
    "workbench.editor.autoLockGroups": {
        "workbench.input.interactive": true //交互窗口
    },
    "files.autoGuessEncoding": true, //编辑器将尝试在打开文件时猜测字符集编码。还可以按语言配置此设置.
    "files.encoding": "gbk" //在读取和写入文件时使用的默认字符集编码。
}

Notice

The small black window popped up by the system is gbk encoding (chcp 936) You can check the encoding format with chcp

Use chcp + corresponding encoding code to temporarily change the encoding format, chcp 936 to change to gbk format

The default encoding of vscode is utf-8, and its terminal is also (chcp 65001)

The encoding format is different, and sometimes Chinese characters will be garbled

The file is garbled, we can choose the correct encoding format and reopen it, it will be normal

But it is troublesome to output garbled characters. After stepping on the pit, my code can avoid most of the garbled characters

I set up three output methods, two are terminal output, one is pop-up window output, recommended pop-up box (basically no problem)

 You can start it in the upper right corner, or you can start it in the upper left corner. The left one is recommended (with a drop-down box)

 You can also debug with the F5 shortcut key

When using the terminal to output, there should be no pop-up window, otherwise, the characters in the pop-up window will be garbled, but the terminal will stand normally

give a chestnut

 

 What should we do at this time?

There are pop-up windows, so I must use pop-up window output

Therefore, the pop-up window is the first to be recommended, but it can be used normally

(If this doesn't work, then I can't help it, the rookie level is here, and I won't be too high) 

Master Edition

Xiaobai advanced version, to be modified is the same, but there are many more things, there are only two major changes, and there are two identical ones, take it from above

The core content is similar (the small white version is refined here), and the advantage is that there are many comments and optional parameters

launch.json
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    // launch.json指导了运行和调试。
    // 用调试器gdb我们就可以逐步运行我们的程序并且监视运行的情况,从而找出bug。
    {
        "version": "0.2.0",
        "configurations": [
            //配置列表。使用 IntelliSense 添加新配置或编辑现有配置。
            {
                "name": "弹窗输出", //配置名称;显示在启动配置下拉菜单中。
                "type": "cppdbg", //配置类型。 cppdbg: C++ (GDB/LLDB)
                "request": "launch", //请求配置类型。可以是“启动”或“附加”。
                // 第一种写法,另一个看下面的案例
                "program": "C:\\windows\\System32\\cmd.exe", //打开cmd
                "args": [
                    "/C",
                    "${fileDirname}\\${fileBasenameNoExtension}.exe", //程序可执行文件的完整路径。
                    "&", // 连接多个命令
                    "pause" //避免程序运行完后闪退,相当于c++的system("pause");
                ],
                "stopAtEntry": false, //可选参数。如果为 true,则调试程序应在目标的入口点处停止。如果传递了 processId,则不起任何作用。
                "cwd": "${workspaceFolder}", //目标的工作目录。
                "environment": [ //要添加到程序环境的环境变量
                    //比如: [ { "name": "config", "value": "Debug" } ],而不是 [ { "config": "Debug" } ]。
                ],
                "externalConsole": true, //如果为 true,则为调试对象启动控制台。如果为 false,它在 Linux 和 Windows 上会显示在集成控制台中。
                "MIMode": "gdb", //指示 MIDebugEngine 要连接到的控制台调试程序。允许的值为 "gdb"、"lldb"。
                "miDebuggerPath": "F:\\vscode\\MSY\\MSY2\\ucrt64\\bin\\gdb.exe", //MI 调试程序(如 gdb)的路径。如果未指定,将首先在路径中搜索调试程序。
                /*修改成自己bin目录下的gdb.exe,这里的路径和电脑里复制的文件目录有一点不一样,这里是两个反斜杠\\*/
                "setupCommands": [ //为了安装基础调试程序而执行的一个或多个 GDB/LLDB 命令
                    {
                        "description": "为 gdb 启用整齐打印", //此命令的可选说明。
                        "text": "-enable-pretty-printing", //要执行的调试命令。
                        "ignoreFailures": true //此命令的可选说明。
                    }
                ],
                "preLaunchTask": "mytask" //调试会话开始前要运行的任务,是之前在task里的第一个label(自定义名字)
            },
            {
                "name": "终端输出",
                "type": "cppdbg",
                "request": "launch",
                //第二种写法
                "program": "${fileDirname}\\${fileBasenameNoExtension}.exe", //程序可执行文件的完整路径。
                "args": [
                    //传递给程序的命令行参数。
                ],
                "stopAtEntry": false, //可选参数。如果为 true,则调试程序应在目标的入口点处停止。如果传递了 processId,则不起任何作用。
                "cwd": "${fileDirname}", //目标的工作目录。
                "environment": [], //要添加到程序环境的环境变量
                "externalConsole": false, //默认false,即不打开小黑窗,直接在vscode终端输出,vscode右上角的就是如此
                // 注意 弹出窗口是系统的,gbk编码,终端的是默认utf-8,中文可能会乱码,嫌麻烦可以直接改true
                "MIMode": "gdb", //指示 MIDebugEngine 要连接到的控制台调试程序。允许的值为 "gdb"、"lldb"。
                "miDebuggerPath": "F:\\vscode\\MSY\\MSY2\\ucrt64\\bin\\gdb.exe", //MI 调试程序(如 gdb)的路径。如果未指定,将首先在路径中搜索调试程序。
                /*修改成自己bin目录下的gdb.exe,这里的路径和电脑里复制的文件目录有一点不一样,这里是两个反斜杠\\*/
                "setupCommands": [ //为了安装基础调试程序而执行的一个或多个 GDB/LLDB 命令
                    {
                        "description": "为 gdb 启用整齐打印", //任务说明
                        "text": "-enable-pretty-printing", //要执行的调试命令
                        "ignoreFailures": true //如果为 true,应忽略此命令的失败。默认值为 false。
                    },
                    {
                        "description": "将反汇编风格设置为 Intel", //任务说明
                        "text": "-gdb-set disassembly-flavor intel", //要执行的调试命令
                        "ignoreFailures": true //如果为 true,应忽略此命令的失败。默认值为 false。
                    }
                ],
                // "preLaunchTask": { //调试会话开始前要运行的Grunt任务。
                //     "task": "mytask",//要自定义的Grunt 任务。
                //     "type": "grunt",
                //     "args": [],//要传递给 grunt 任务的命令行参数
                //     "file": ""//提供任务的 Grunt 文件。可以省略。
                // }, 
                "preLaunchTask": "mytask" //调试会话开始前要运行的任务,是之前在task里的第一个label(自定义名字)
            }
        ]
    }
tasks.json
// tasks.json会指导vsc编译、运行你的文件。
// See https://go.microsoft.com/fwlink/?LinkId=733558  // 查看官方文档
// vsc会为你默认生成一个模板,如果你安装了C/C++ Extension,intellisense会为你解释每一行的意义。
// 悬停以查看现有属性的描述。
// ${file}	你窗口正在显示的这个文件
// ${fileDirname}	${file}所在的文件夹
// ${fileBasenameNoExtension}	去掉文件尾名的文件名
// ${workspaceFolder}	就是你最开始打开的文件夹,大写名字的那个
{
    "version": "2.0.0", // 配置的版本号。
    "tasks": [ //任务配置。通常是外部任务运行程序中已定义任务的扩充。
        {
            "type": "shell", //定义任务是被作为进程 process 运行还是在 shell 中作为命令运行。
            "label": "mytask", //取个名字,不过要记得,待会 launch.json 的 preLaunchTask 要用
            "command": "F:\\vscode\\MSY\\MSY2\\ucrt64\\bin\\g++.exe", //执行编译的编译器或脚本的路径 //g++ 适合于C++。
            "args": [ //方括号里是传给g++的命令行参数
                "-g", //指定输出文件的路径和名称
                "${file}", //指定要编译的是当前文件
                "-o", //指定输出文件的路径和名称
                "${fileDirname}\\${fileBasenameNoExtension}.exe", //承接上一步的-o,让可执行文件输出到源码文件所在的文件夹下的bin文件夹内,并且让它的名字和源码文件相同
                "-std=gnu++17", // 语言标准
                "-fexec-charset=GBK", // 生成的程序使用GBK编码
                "-fdiagnostics-color=always", //用于在编译时启用彩色输出以改善可读性
                // "-I", //头文件(自己写的)
                // "-lfunc", //外部库的名字
                // "-L./", //外部库的路径
                // "-Wall", // 开启额外警告
                // "-static-libgcc", // 静态链接libgcc
                // "F:\\vscode\\my vscode",
            ],
            "presentation": { //配置用于显示任务输出并读取其输入的面板。
                "echo": true, //控制是否将执行的命令显示到面板中。
                "reveal": "always", //控制运行任务的终端是否显示。可按选项 "revealProblems" 进行替代。
                "focus": false, //控制面板是否获取焦点。默认值为“false”。如果设置为“true”,面板也会显示。
                "panel": "new", // 控制是否在任务间共享面板。同一个任务使用相同面板还是每次运行时新创建一个面板。
                //new 每个进程创建新窗口
                "showReuseMessage": false, //控制是否显示“终端将被任务重用,按任意键关闭”提示。
                "clear": false //控制是否在执行任务之前清除终端。
            },
            "options": { // 其他命令选项
                "cwd": "${fileDirname}", //已执行程序或脚本的当前工作目录。如果省略,则使用代码的当前工作区根。
                "env": {}, //已执行程序或 shell 的环境。如果省略,则使用父进程的环境。
                // "shell": {} // 配置使用的 shell。
            },
            "problemMatcher": [
                //要使用的问题匹配程序。可以是一个字符串或一个问题匹配程序定义,也可以是一个字符串数组和多个问题匹配程序。
                "$gcc"
            ],
            "group": "build", //  将任务标记为可通过 "运行生成任务" 命令访问的生成任务。
            "detail": "弹窗内输出" // 任务的其他详细信息。
        },
        { // 设置与上面的性质相同,都是构建任务
            "type": "shell",
            "label": "myrun",
            "command": "F:\\vscode\\MSY\\MSY2\\ucrt64\\bin\\g++.exe", //改成自己的路径
            "args": [ //其他参数见上一个
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "presentation": { //配置用于显示任务输出并读取其输入的面板。
                "echo": true, //控制是否将执行的命令显示到面板中。
                "reveal": "always", //控制运行任务的终端是否显示。可按选项 "revealProblems" 进行替代。
                "focus": false, //控制面板是否获取焦点。默认值为“false”。如果设置为“true”,面板也会显示。
                "panel": "new", // 控制是否在任务间共享面板。同一个任务使用相同面板还是每次运行时新创建一个面板。
                //new 每个进程创建新窗口
                "showReuseMessage": false, //控制是否显示“终端将被任务重用,按任意键关闭”提示。
                "clear": false //控制是否在执行任务之前清除终端。
            },
            "options": { // 其他命令选项
                "cwd": "${fileDirname}", //已执行程序或脚本的当前工作目录。如果省略,则使用代码的当前工作区根。
                "env": {}, //已执行程序或 shell 的环境。如果省略,则使用父进程的环境。
                // "shell": {} // 配置使用的 shell。
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build",
            "detail": "终端内输出"
        },
        { //这个大括号里是‘运行(run)’任务,一些设置与上面的构建任务性质相同,此任务是组中的默认任务,vscode右上角三角形运行
            "type": "cppbuild", // 要自定义的任务类型
            "label": "run", //任务名称,点vscode 右上角运行的就是这个
            "command": "F:\\vscode\\MSY\\MSY2\\ucrt64\\bin\\g++.exe", //执行编译的编译器或脚本的路径。
            "args": [ //其他要传递给编译器或编译脚本的参数
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe", //执行exe文件,只需要指定这个exe文件在哪里就好
            ],
            "options": { // 其他命令选项
                "cwd": "${fileDirname}", //已执行程序或脚本的当前工作目录。如果省略,则使用代码的当前工作区根。
                "env": {}, //已执行程序或 shell 的环境。如果省略,则使用父进程的环境。
                // "shell": {}  // 配置使用的 shell。
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "test", //这一组是‘测试’组,将run任务放在test组里方便我们用快捷键执行
                "isDefault": true // 定义此任务是组中的默认任务,还是与应触发此任务的文件匹配的 glob。
            },
            "detail": "调试器生成的任务。" // 任务的其他详细信息。
        }
    ]
}

Guess you like

Origin blog.csdn.net/m0_74921567/article/details/132324926