vscode c++ 配置

Posted 爬~就en爬

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vscode c++ 配置相关的知识,希望对你有一定的参考价值。

//vscode C++环境配置

常用插件 直接安装即可 安装完成后重启

cmake ;cmake tools;chinese (Simplified); c/c++;

关于环境配置的前提 MinGW

下载最新版本安装就可

//三个配置文件

c_cpp_properties.json


    "configurations": [
        
            "name": "Win32",
            "includePath": [
                "$workspaceFolder/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "你的本地路径/g++.exe",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "windows-gcc-x64"
        
    ],
    "version": 4

launch.json


    "version": "0.2.0",
    "configurations": [

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

tasks.json


    "version": "2.0.0",
    "tasks": [
        
            "label": "g++",
            "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": "build"
        ,
        
            "type": "cppbuild",
            "label": "C/C++: g++.exe 生成活动文件",
            "command": "你的路径g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "$file",
                "-o",
                "$fileDirname\\\\$fileBasenameNoExtension.exe"
            ],
            "options": 
                "cwd": "你的路径/bin"
            ,
            "problemMatcher": [
                "$gcc"
            ],
            "group": 
                "kind": "build",
                "isDefault": true
            ,
            "detail": "调试器生成的任务。"
        
    ]

程序运行

程序编写结束后,ctrl+s保存,F5运行程序

vscode配置C++多个.cpp文件

1配置文件

一般vscode配置C++有三个文件,它们分别是:

1.1.c_cpp_properties.json

设置编译环境,通过Ctrl+Shift+P,输入C++,在下拉菜单中选择“C/C++ Edit configuration”,系统自动会在.vscode目录下创建该文件,供我们设置编译环境。可根据自己需求改动如下配置,默认配置如下:

{
    "configurations": [
        {
            "name": "Win32", // 环境名称
            "includePath": [ // 头文件包含路径,当前指定的是工作目录,有需要可添加,加入相应的文件路径即可
                "${workspaceFolder}/**"
            ],
            "defines": [     // 预处理定义
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "C:\\mingw64\\bin\\gcc.exe", // 编译器路径
            "cStandard": "gnu17",  // 设置C标准库
            "cppStandard": "gnu++14", // 设置C++标准库
            "intelliSenseMode": "gcc-x64" // 设置补全模式
        }
    ],
    "version": 4
}

 

1.2.tasks.json

设置编译参数,通过Ctrl+Shift+p,输入task,在下拉菜单中选择Tasks: Configure Default Build Task -> Create task.json file from templates -> Others,系统自动在.vscode下创建task.json文件,供我们设置具体的编译规则。根据实际请求修改如下:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build C++ train",
            "type": "shell",
            "command": "g++",
            "args": ["-g", "${workspaceFolder}/*.cpp"  , "-o", "train"],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

  

 

3.launch.json

以上是关于vscode c++ 配置的主要内容,如果未能解决你的问题,请参考以下文章

小白学习C++ 教程一Vscode配置C++环境

mac vscode配置c++ debug环境

windows下配置VScode,写C++, 使用opencv库和openMVG依赖库 : windows下, VSCode配置C++环境

C++学习笔记IDE配置的学习笔记

VScode配置c或c++环境

编译器2022-11-VSCode配置编译与调试C++程序(含输入输出重定向)