记录vscode配置c++环境

1.安装mingw64并配置环境变量,确保电脑安装上了编译器,可以在终端用用gcc -v 或 g++-v 或gdb -v 查看版本并验证是否安装好。

2.在vscode中安装C/C++插件和中文插件,在搜索栏输入“>C/C++”然后进入C/C++插件的UI界面配置编译器,gcc对应c语言,g++对应c++,自行选择

 

3.最主要的的是三个json文件的生成,第一个编译器配置文件,可以在C/C++插件的UI界面配置好后自动生成,选择好的就会体现在json文件的字段中

 

 1 {
 2     "configurations": [
 3         {
 4             "name": "Win32",
 5             "includePath": [
 6                 "${workspaceFolder}/**"
 7             ],
 8             "defines": [
 9                 "_DEBUG",
10                 "UNICODE",
11                 "_UNICODE"
12             ],
13             "compilerPath": "D:/mingw64/bin/g++.exe",
14             "cStandard": "c11",
15             "cppStandard": "c++17",
16             "intelliSenseMode": "gcc-x64"
17         }
18     ],
19     "version": 4
20 }

4.tasks.json,在搜索框输入“>tasks”,选择下图,生成tasks.json

 


{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe 生成活动文件",
            "command": "D:/mingw64/bin/g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "D:/mingw64/bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "编译器: D:/mingw64/bin/g++.exe"
        }
    ]
}

3.点一下左侧工具栏的运行按钮,自动生成launch.json,修改内容如下

 

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: 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": true,
            "MIMode": "gdb",
            "miDebuggerPath": "D:/mingw64/bin/gdb.exe",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++.exe 生成活动文件"
        }
    ]
}

 4.测试程序

#include <iostream>
int main()
{
    std::cout<< "hello vscode"  <<std::endl;
    system("pause");
}

 

 

 

 

 

 

 

 

 

 


posted @ 2024-03-04 00:01  变秃了也就变强了  阅读(15)  评论(0编辑  收藏  举报