那些年C语言的坑(vscode如何运行C语言)

VS Code运行C语言的配置


记一下花费考研期间几个星期走过的坑:

  1. mingw64的安装时间长到令人窒息,绝逼是个坑,最后在网上找到了压缩包,可快速解压即可使用,但是网站不记得了。
  2. 网上的教程一大堆,行不通的一大堆,别人的配置是别人的配置,用合适自己的,而且每个人的文件路径也不一样。
  3. Clion的编译时间也是可以让你等到分分钟卸载,每次创建新文件还得cmakelist类似目录,添加可执行文件名称。886
  4. DevC++听说已经停止维护很久了,而且每次还得在编译选项中选择C语言的规则还是C++的。

配置过程正文

  1. 下载VS Code(Visual Studio Code),不是VS(Visual Studio2017,因为这个软件很庞大,使用更麻烦,不推荐使用)
  2. 下载C语言的编译环境,有MinGW64,TCC,Cygwin…等等
  3. 在VScode下载插件C/C++,code runner插件
  4. mingw64/bin的路径装到环境变量(其他编译环境也一样,文件路径的目录下要有gcc.exe,g++.exe之类的文件)
    .

法一(针对Code Runner插件)
setting.json

{
    "editor.fontSize": 16,
    "code-runner.runInTerminal": true,
    "terminal.integrated.fontSize": 15,
    "terminal.integrated.shell.windows": "C:\\Windows\\System32\\cmd.exe",
    "code-runner.executorMap": {
        "c": "cd $dir && gcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt"
    },
}

法二(针对微软的C/C++插件)
在项目中创建.vscode文件,下面是需要包含的文件

  1. launch.json
  2. tasks.json
  3. c_cpp_properties.json

一般只需要改下面编译器的文件路径,我是使用了法一,法二虽然也成功了但是不了解为什么这样。
launch.json

{
    "version": "0.2.0",
    "configurations": [
      {
        "name": "(gdb) Launch",
        "preLaunchTask": "Build",
        "type": "cppdbg",
        "request": "launch",
        "targetArchitecture": "x86_64",
        "program": "${fileDirname}/${fileBasenameNoExtension}.exe",
        "miDebuggerPath": "D:\\MinGW64\\bin\\gdb.exe",
        "args": [],
        "stopAtEntry": false,
        "cwd": "${workspaceFolder}",
        "environment": [],
        "externalConsole": true,
        "MIMode": "gdb",
        "setupCommands": [
          {
            "description": "Enable pretty-printing for gdb",
            "text": "-enable-pretty-printing",
            "ignoreFailures": true
          }
        ]
      }
    ]
}

tasks.json

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "Build",
      "type": "shell",
      "presentation": {
        "echo": true,
        "reveal": "always",
        "focus": false,
        "panel": "shared"
      },
      "windows": {
        "command": "g++",
        "args": [
          "-ggdb",
          "\"${file}\"",
          "--std=c++11",
          "-o",
          "\"${fileDirname}\\${fileBasenameNoExtension}.exe\""
        ]
      },
      "group":{
        "kind": "build",
        "isDefault": true
      }
    }
  ]
}

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "D:/MinGW64/include/*"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "D:/MinGW64/bin/gcc.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "gcc-x64"
        }
    ],
    "version": 4
}

也可参考https://blog.csdn.net/qq_28581077/article/details/81380341
看着还行

  • 3
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值