Fork me on GitHub

VSCode配置配置C++环境

GCC环境

首先安装gcc环境
https://www.cnblogs.com/aeolian/p/14562572.html

Win+R输入mingw-get安装管理器,安装gdb,或者命令行输入mingw-get install gdb

VSCode配置

配置编译环境

VSCode中 Ctrl+Shift+P调出命令面板,输入C/C++,选择“Edit Configurations(UI)”进入配置。
image
编译器路径:配置MinGW安装路径下的g++.exe,IntelliSense 模式:gcc-x64
image

生成配置文件

快捷键Ctrl+Shift+P调出命令面板,输入tasks,选择“Tasks:Configure Default Build Task”,再选择“C/C++: g++.exe build active file”,然后会产生task.json和launch.json两个文件。
image

修改配置文件

修改下面g++.exe的路径并粘贴到task.json中。

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "g++.exe build active file",
            "command": "D:\\ProgramFiles\\MinGW\\bin\\g++.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "D:\\ProgramFiles\\MinGW\\bin\\"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

修改下面g++.exe的路径并粘贴到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": "(gdb) Launch",
            "preLaunchTask": "g++.exe build active file",
            "type": "cppdbg",//只能为cppdbg
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",//调试程序的路径名称
            "args": [],//调试传递参数
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "D:\\ProgramFiles\\MinGW\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

测试

#include <stdio.h>
#include <stdlib.h>   //引入头文件使用system方法

int main(){
    printf("Hello World!");
    system("pause");  //防止cmd一闪而过
    return 0;
}

运行后弹出
image

其他配置

使用Unicode字符集

头部加上如下代码即可

#define UNICODE
#define _UNICODE

在使用UNICODE时,字符串前面带L表示转换成宽字符,就是每个字符占用两个字节。

CMD乱码

CMD窗口之所以乱码是因为VSCode中文件是以UTF-8保存的,而CMD是以GBK形式编码字符的。

解决方法是在tasks.json中指定g++.exe编译cpp时使用什么编码格式,tasks -> args 添加如下参数。

"-fexec-charset=GBK",

include<stdio.h>头文件红色

image
打开c_cpp_properties.json,添加include路径,放在最上面
image

posted @ 2021-03-25 20:51  秋夜雨巷  阅读(259)  评论(0编辑  收藏  举报