Win10下配置VScode的C/C++开发环境

1. MinGW安装配置

MinGW下载安装

MinGW是什么这里就不进行介绍了,下面直接进入安装的正题。MinGW的下载链接:https://osdn.net/projects/mingw/releases/, 在网站中可以下载到mingw-get-setup.exe安装文件。
安装的时候根据自己的需求进行设置,不知道如何设置的可以直接使用默认值,安装完成后会自动打开MinGW Installing Manager,如下:

这里我们主要配置C/C++开发环境,所以选择minw32-gcc-g++即可,当然你可以全部选中。然后点击菜单栏"Installation"-"Apply changes"应用更改。具体操作可参照MinGW安装教程[TZZ]

设置环境变量

安装完成MinGW后将其添加至环境变量,即把C:\MinGW\bin添加至系统环境变量。


注意:这里的C:\MinGW\bin与你的安装路径有关!

下面在命令提示符或者Windows PowweShell中输入g++ -v测试是否配置成功,如果得到如下结果则表示成功:

2. Vscode安装配置

Vscode安装

在Vscode官网下载安装包:https://code.visualstudio.com/

安装完成后打开Vscode,主界面如下:

安装C/C++插件

点击左侧Extension按钮来选择安装插件:

搜索C++,点击Install

新建C/C++工程

由于Vscode以文件夹的形式管理工程,所以需要新建一个文件夹来管理工程,这里建立一个名为hello的文件夹。

然后用Vscode打开该文件夹:

打开后得到一个空的工程:

快捷键Ctrl+N新建一个C++文件,取名为helloworld.cpp,并输入一个简单的程序:

启动配置文件(launch.json)

点击左边栏的Debug按钮,然后选择配置launch.json文件,操作如下:

选择C++(GDB\LLDB),自动打开launch.json文件:

launch.json初始内容如下:

{
    // 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) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "enter program name, for example ${workspaceFolder}/a.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "/path/to/gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

修改为如下内容并保存:

{
    // 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) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "C:/MinGW/bin/gdb.exe",
            "preLaunchTask": "build",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

注意
"miDebuggerPath": "C:/MinGW/bin/gdb.exe"与你安装的路径有关!
"preLaunchTask": "build"表示设置启动前的任务为"build",下面通过tasks.json来添加"build"任务。

添加编译任务(tasks.json)

利用快捷键ctrl+shift+p打开命令行,输入Tasks: Run task,然后依次选择:
> No task to run found. configure tasks...
> Create tasks.json file from template
> Others Example to run an arbitrary external command.
自动打开的tasks.json文件初始内容如下:

    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "echo",
            "type": "shell",
            "command": "echo Hello"
        }
    ]
}

将其修改为如下内容并保存:

// 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\""
                ]
            }
        }
    ]
}

至此,我们已经完成的C/C++的基本开发环境,可以利用Vscode进行代码编辑、编译、调试等功能。此外,Vscode支持Linux系统,关于在Linux系统下配置基于Vscode的C/C++开发环境可参考另一篇博文:https://blog.csdn.net/weixin_43374723/article/details/84064644

  • 6
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值