栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

     1. 安装MinGW: 

  •  官网:https://osdn.net/projects/mingw/ 
  • 点击下载:mingw-getsetupexe

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

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

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

 

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

  •  配置环境变量: 

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

D:MinGWbin

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

gcc -v

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

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

跑一把程序:

#include 
#include 

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

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

  • 插件运行

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

 勾选Ignore Selection和Run 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 的配置

配置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就可以跑了,还有就是把路径换成对应安装路径。

跑一把程序:

 搞定简简单单有手进行。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/676255.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号