Window10使用VScode搭建C/C++开发环境

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sjdjdjdjahd/article/details/89181076

在网上找了一些安装的教程,到最后都运行失败了。不过也没白找,最起码知道需要安装插件和编译器了。

1、安装编译器:MinGW

MinGW官网进行下载安装。https://sourceforge.net/projects/mingw/files/
我下载的是压缩包,解压就可以使用了。

如果下载的是程序,安装完成后需要打开MinGW Installation Manager,勾选下面两项

在这里插入图片描述
然后点击左上角的install – Apply Changes 安装

环境变量配置

在这里插入图片描述

打开终端,运行 gcc -v 检验是否安装成功

在这里插入图片描述

2、安装VSCode

官网下载安装即可 https://code.visualstudio.com/

安装插件

在这里插入图片描述
使用VSCode打开一个文件夹(路径不包含中文)。新建一个hello.c文件。
写一段hello world。
在这里插入图片描述

F5运行,会出现下面提示,选择(GDB/LLDB)编译:

在这里插入图片描述

构建launch.json,并且将miDebuggerPath修改成自己的路径就行。

在这里插入图片描述

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "gdb_Launcher",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "D:\\VScode\\mingw64\\bin\\gdb.exe",//修改成你自己的gdbl路径
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "gcc.exe build active file"
        }
    ]
}

如果提示构建tasks.json文件的话,就系统构建就行

 "tasks": [
    {
      "type": "shell",
      "label": "gcc.exe build active file",
      "command": "D:\\VScode\\mingw64\\bin\\gcc.exe",
      "args": [
        "-g",
        "${file}",
        "-o",
        "${fileDirname}\\${fileBasenameNoExtension}.exe"
      ],
      "options": {
        "cwd": "D:\\VScode\\mingw64\\bin"
      }
    },
    {
      "type": "shell",
      "label": "gcc.exe build active file",
      "command": "D:\\VScode\\mingw64\\bin\\gcc.exe",
      "args": [
        "-g",
        "${file}",
        "-o",
        "${fileDirname}\\${fileBasenameNoExtension}.exe"
      ],
      "options": {
        "cwd": "D:\\VScode\\mingw64\\bin"
      },
      "problemMatcher": [
        "$gcc"
      ]
    }
  ],
  "version": "2.0.0"

F5运行项目,在控制台查看打印结果

在这里插入图片描述

也可以手动编译运行

gcc test.c -o test.exe将test.c编译成.exe文件
./test.exe运行.exe文件
在这里插入图片描述
大功告成。

第一次使用VSCode的时候,记得设置代码自动保存。不然编译运行会报undefined reference to `WinMain’异常。
关于控制台中文乱码
1、使用GB2312。
 setting - encode - gb2312
 如果还是乱码的话,请新建一个文件。因为之前乱码的文件编码不是gb2312。
2、在.vscode中的setting.json中灵活配置文件编码

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/sjdjdjdjahd/article/details/89181076
今日推荐