Visual Studio Code配置C语言环境笔记

     1. 安装MinGW: 

  •  安装步骤: Install下一步 -> Change选择目录 -> Continue -> Continue -> 安装 -> 配置环境

  • 我安装在D盘MinGW目录,之后配置环境变量需要用到该路径,记住自己安装的目录。

  • 点击Continue下一步,的等待100%安装完成后,继续点Continue下一步。

 

  •  选择第支持支持编译语言 我选的cc++, 选择完后点击左上角Installation后,点击Apply Changes安装完成,点击close关闭即可。
  •  安装完成

  •  配置环境变量: 

        右击此电脑属性 ->  高级系统设置 -> 环境变量 -> path变量(系统或用户的都行,我用系统的)->   点击新建:

D:\MinGW\bin

测试一把查看版本信息:9.2.0

gcc -v

 更多gcc命令菜鸟教程查看:

https://www.runoob.com/w3cnote/gcc-parameter-detail.html

跑一把程序:

#include <stdio.h>
#include <stdlib.h>

int main ()
{
    printf("HelloWorld\n");
    system("pause"); // 防止弹窗闪退
    return 0;
}
编译:gcc hello_world.c -o hello_world.exe
运行:./hello_world.exe

      2. 配置Visual Studio Code 2种运行方式

  • 插件运行

下载:Code Runner 安装完成左上角会出现一个运行图标,然后点击设置点击控制设置。

 勾选Ignore SelectionRun In Terminal 

或者打开配置文件settings.json直接添加

 "code-runner.ignoreSelection": true,
 "code-runner.runInTerminal": true

在.vscode文件夹添加配置文件:tasks.json

注:把MinGW各路径换成你安装的路径,args配置项目运行exe程序,正确配置自己的路径,我的.exe文件和.c文件是放同一个工作区下的同一个文件夹所以写成:

"${workspaceFolder}\\${fileBasenameNoExtension}\\${fileBasenameNoExtension}.exe"

 如果不是自己配置一下变量即可(该文有变量注解):

VSCODE中各种预定义变量汇总_endurehero的博客-CSDN博客_vscode变量

{
    "version": "2.0.0",
    "command": "gcc",
    "args": [
        "-g","-std=c++11","${file}",
        "-o","${workspaceFolder}\\${fileBasenameNoExtension}\\${fileBasenameNoExtension}.exe"
    ],
    "problemMatcher": {
        "owner": "c",
        "fileLocation": ["relative", "${workspaceFolder}"],
        "pattern": {
            "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
            "file": 1,
            "line": 2,
            "column": 3,
            "severity": 4,
            "message": 5
        }
    },
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: gcc.exe 生成活动文件",
            "command": "D:\\MinGW\\bin\\gcc.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileBasenameNoExtension}.exe",
                "-fexec-charset=UTF-8"//解决中文乱码
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "编译器: D:\\MinGW\\bin\\gcc.exe"
        }
    ]
}

完成来一把:

  • 窗口运行Ctrl+F5 的配置,需要在程序里加    system("pause"); 否则闪退!!!

配置launch.json:

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387

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

配置:c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "D:\\MinGW\\include\\**",
                "D:\\MinGW\\include\\ddk\\**",
                "D:\\MinGW\\include\\gdiplus\\**",
                "D:\\MinGW\\include\\GL\\**",
                "D:\\MinGW\\include\\sys\\**",
                "${workspaceRoot}"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "__GNUC__=6",
                "__cdecl=__attribute__((__cdecl__))"
            ],
            
            "browse": {
                "path": [
                    "D:\\MinGW\\include\\**",
                    "D:\\MinGW\\include\\ddk\\**",
                    "D:\\MinGW\\include\\gdiplus\\**",
                    "D:\\MinGW\\include\\GL\\**",
                    "D:\\MinGW\\include\\sys\\**",
                    "${workspaceRoot}"
                ],
                "limitSymbolsToIncludedHeaders": true,
                "databaseFilename": ""
            },
            "compilerPath": "D:\\MinGW\\bin\\gcc.exe",
            "intelliSenseMode": "gcc-x64",
            "cStandard": "c17",
            "cppStandard": "c++17"
            
        }
    ],
    "version": 4
    
}

一般配置完launch.json就可以跑了,还有就是把路径换成对应安装路径。

跑一把程序:

 搞定简简单单有手进行。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZiWie丶ZHANG

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值