Vscode C环境配置

本文链接:https://blog.csdn.net/qq_43067190/article/details/82117149,https://blog.csdn.net/qq_43067190/article/details/82117149

 

Vscode C环境配置 
最近迷上了vscode这款编译器,小巧美观,用起来也很顺手,最主要的是全平台。 
环境: 
WIN10 64 专业版 
vscode版本:1.24.1 
launch.json版本:0.2.0 
tasks.json版本:2.0.0 
mingw-w64版本:8.1.0 
配置过程: 
一、 安装vscode 
vscode官网下载安装包直接安装即可 
二、 vscode内安装C/C++ 插件 
vscode内按快捷组合键Ctrl+Shift+X(或如图点击[拓展]按钮)打开拓展分页,在搜索栏输入” C “,查找到如图的第一个插件,安装并重新加载之。再推荐几个插件,包括彩虹括号和汉化。 
è¿éåå¾çæè¿°

è¿éåå¾çæè¿°

 

 

è¿éåå¾çæè¿°

 

 

è¿éåå¾çæè¿°

 

 

三、 安装mingw-w64(具体安装与环境变量配置可以查看这里) 
在mingw-w64官网下载64位的mingw-w64离线包 
https://sourceforge.net/projects/mingw-w64/files/?source=navbar 
根据系统选择合适的安装包进行下载(win10_64位选择如图标签) 

 

 

è¿éåå¾çæè¿°

下载完成后出现安装包 
安装该包,在Setting 界面将Architecture选项改为x86_64,其他不变,选择合适的安装路径(默认或重新指定都可以,路径中不要有中文) 
也可以直接下载文件压缩包(我是下载文件压缩包直接解压就可以用了) 

 

è¿éåå¾çæè¿°

è¿éåå¾çæè¿°

配置计算机环境变量如图(我的解压路径是C:\Program Files\mingw-w64\x86_64-8.1.0-release-posix-sjlj-rt_v6-rev0,因此环境变量这么加) 
è¿éåå¾çæè¿°

安装完成后打开控制台,分别输入 g++ -v 和 gcc -v、gdb -v 查看环境是否安装成功(是否有当前版本号) 

è¿éåå¾çæè¿°

 

è¿éåå¾çæè¿°

 

四、重启电脑(重要)。 
五、配置运行环境 
打开vscode,选择或新建一个空文件夹目录打开作为项目目录。 
点击“文件”按钮,再点击“新建文件夹”按钮,并重命名为”.vscode”。 
在该文件夹内,在点击“新建文件”按钮,建launch.json,settings.json,tasks.json三个.json文件。如图所示。 
 

è¿éåå¾çæè¿°

 

launch.json的文件内容如下: 
可以直接复制,并修改有注释的一段

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "preLaunchTask": "build",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "C:/Program Files/mingw-w64/x86_64-8.1.0-release-posix-sjlj-rt_v6-rev0/mingw64/bin/gdb.exe", // 这里修改GDB路径为安装的mingw64的bin下的gdb.exe路径
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true,
                }
            ]
        }]
}

tasks.json的文件内容如下:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": false,
                "panel": "shared"
            },
            "windows": {
                "command": "g++",
                "args": [
                    "-ggdb",
                    "\"${file}\"",
                    "--std=c++11",
                    "-o",
                    "\"${fileDirname}\\${fileBasenameNoExtension}.exe\"",
                    "-finput-charset=UTF-8",//输入编译器文本编码 默认为UTF-8
                    "-fexec-charset=GBK"//编译器输出文本编码 自行选择
                ]
            }
        }
    ]
}

settings.json的文件内容如下: 
用户设置为:

 

// Configuring tasks.json for C/C++ debugging 
// author: huihut
// repo: https://gist.github.com/huihut/887d3c28db92617bd5148c20a5ff112a

// Available variables which can be used inside of strings.
// ${workspaceRoot}: the root folder of the team        
// ${file}: the current opened file                     
// ${fileBasename}: the current opened file's basename 
// ${fileDirname}: the current opened file's dirname    
// ${fileExtname}: the current opened file's extension  
// ${cwd}: the current working directory of the spawned process


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

    },
    "files.encoding": "utf8",
    "files.autoGuessEncoding": true,
    "explorer.confirmDragAndDrop": false,
    "workbench.colorTheme": "Visual Studio Dark",
    "team.showWelcomeMessage": false
}

 

工作区设置:

{
    "C_Cpp.errorSquiggles": "Disabled",
    "files.associations": {
        "stdlib.h": "c",
        "time.h": "c"
    }
}

至此,环境配置完成。 
六、运行C代码 
新建helloworld.c文件,键入或粘贴C语言的helloworld代码,按F5调试运行。

#include <stdio.h>
#include <windows.h>
int main() {
    printf("hello world!\n\n");
    system("pause");
    return 0;
}

è¿éåå¾çæè¿°

代码中system(“pause”);语句是“请按任意键继续….”,没有此句,调试窗口将一闪就退出。

  • 10
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值