使用VSCode运行C语言

VSCode安装

下载地址:https://code.visualstudio.com/Download

Mingw-w64 下载

下载地址:https://sourceforge.net/projects/mingw-w64/files/mingw-w64/mingw-w64-release/

下载并解压后配置PATH

安装VSCode 插件

配置运行文件

  1. 新建一个工程,然后建一个.c文件,随便写点代码,运行它,.vscode文件夹,文件夹下会生成launch.json文件,另外可以通过在该文件夹下新建 tasks.json。
  2. 配置 launch.json文件
  {
      "configurations": [
          {
              "name": "(gdb) Launch", // 配置名称,将会在启动配置的下拉菜单中显示
              "type": "cppdbg", // 配置类型,这里只能为cppdbg
              "request": "launch", // 请求配置类型,可以为launch(启动)或attach(附加)  
              "program": "${fileDirname}\\${fileBasenameNoExtension}.exe", // 将要进行调试的程序的路径 
              "stopAtEntry": false, // 设为true时程序将暂停在程序入口处,一般设置为false
              "cwd": "${workspaceFolder}", // 调试程序时的工作目录,一般为${workspaceFolder}即代码所在目录 
              "environment": [],
              "externalConsole": false, // 如果为true,则为调试对象启动控制台。如果为false,它在Linux和Windows上会显示在集成控制台中
              "MIMode": "gdb",
              "miDebuggerPath": "D:\\软件安装位置\\mingw64\\bin\\gdb.exe", // miDebugger的路径,注意这里要与MinGw的路径对应
              "preLaunchTask": "gcc",
              "setupCommands": [
                  {
                      "description": "为 gdb 启用整齐打印",
                      "text": "-enable-pretty-printing",
                      "ignoreFailures": true
                  }
              ]
          }
      ]
  }
  1. 配置 task.json文件
{
    "version": "2.0.0",
    "command": "gcc",
    "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": {
        "kind": "build",
        "isDefault": true
    }
}
  1. 配置 c_cpp_properties.json 文件
{
    "configurations": [{
        "name": "MinGW64",
        "includePath": [
            "${workspaceFolder}/**",
            "D:/软件安装位置/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
            "D:/软件安装位置/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
            "D:/软件安装位置/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/include"
        ],
        "defines": [
            "_DEBUG",
            "UNICODE",
            "_UNICODE"
        ],
        "compilerPath": "D:\\软件安装位置\\mingw64\\bin\\gcc.exe",
        "cStandard": "c11",
        "cppStandard": "c++17",
        "intelliSenseMode": "gcc-x64"
    }],
    "version": 4
}
  1. 运行
#include <stdio.h>
#include <string.h>

int main()
{
    printf("hello world");
    return 0;
}

相关

下一页
上一页