VsCode C/C++ 环境

时间:2020-05-27
本文章向大家介绍VsCode C/C++ 环境,主要包括VsCode C/C++ 环境使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

下载 MinG-W64,选最新版本中的 x86_64-posix-seh,然后解压,设置环境变量。

https://sourceforge.net/projects/mingw-w64/files/

下载 VsCode 相关插件

只有 c/c++ 是必须的。

在工作区创建 .vscode 文件夹,用 VsCode 创建,系统自带文件管理无法创建以点开头的文件夹,然后创建下面两个配置文件

修改配置中的路径为自己的 Ming-W64 路径

launch.json

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

tasks.json

{
    "version": "2.0.0",
    "command": "gcc",
    "args": [
        "-g",
        "${file}",
        "-o",
        ".target/${fileBasenameNoExtension}.exe"
    ], // 编译命令参数
    "problemMatcher": {
        "owner": "cpp",
        "fileLocation": [
            "relative",
            "${workspaceFolder}"
        ],
        "pattern": {
            "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
            "file": 1,
            "line": 2,
            "column": 3,
            "severity": 4,
            "message": 5
        }
    },
    "presentation": {
        "echo": true,
        "reveal": "always",
        "focus": false,
        "panel": "new", // 这里 shared 表示共享,改成 new 之后每个进程创建新的端口
        "showReuseMessage": true,
        "clear": false
    }
}

测试

#include <math.h>
#include <stdio.h>

void main() {
  double a = pow(2.0, 3.0);
  printf("res = %f.2\n", a);
  printf("hello\n");

  double b = 1.234;
  int c = b;
  printf("%d", c);
}

按 F5 即可调试运行

想直接运行,也可以安装 code-runner 插件

{
    "editor.minimap.enabled": false,
    "editor.mouseWheelZoom": true,
    "files.autoSave": "afterDelay",
    "terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
    "liveServer.settings.donotShowInfoMsg": true,
    "workbench.startupEditor": "newUntitledFile",
    "code-runner.ignoreSelection": true,
    "code-runner.runInTerminal": true,
    "editor.fontSize": 18,
    "liveServer.settings.multiRootWorkspaceName": "",
    "git.path": "D:/PcAPP/PortableGit/bin/git.exe",
    "git.enabled": false,
    "[c]": {
        "editor.defaultFormatter": "ms-vscode.cpptools"
    },
    "C_Cpp.clang_format_fallbackStyle": "LLVM",
    "C_Cpp.intelliSenseEngine": "Tag Parser",
    "workbench.colorTheme": "IDEA like light Theme",
    "code-runner.executorMap": {
        "c": "cd $dir && gcc $fileName -o ..\\.target\\$fileNameWithoutExt.exe && ..\\.target\\$fileNameWithoutExt.exe",
        "cpp": "cd $dir && g++ $fileName -o ..\\.target\\$fileNameWithoutExt.exe && ..\\.target\\$fileNameWithoutExt.exe"
    },
    "workbench.iconTheme": "vsclassic-icon-theme",
    "update.showReleaseNotes": false,
    "telemetry.enableTelemetry": false,
}

附上 VsCode 设置

原文地址:https://www.cnblogs.com/jhxxb/p/12974321.html