window10下vscode配置C/C++开发环境

最近迷上了vscode,小巧美观,最主要的是全平台。但是vscode并不是ide,必须得自己配置环境。网上有很多相关的帖子,但是很多介绍的并不是很详细。so, 在此贴出自己的配置过程。

一、vscode安装C/C++ 插件

 

在此之前需要讲一下vscode界面一些布局。

选择插件安装,或者vscode内按快捷组合键Ctrl+Shift+X。

按照下列步骤1--->2--->3,由于我已经安装了,所以是uninstall,没有安装时是install,点击安装就好。

二、配置C\C++环境

mingw-w64官网下载64位的mingw-w64在线安装包(以在线包为例)或离线包(离线包直接解压出来就能用)

在线包:根据系统选择合适的安装包进行下载,选择在线安装器。

下载之后安装好,把bin路径配置到环境变量path中。

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

三、运行C++代码

打开vscode,选择或新建一个空文件夹目录打开作为项目目录,新建一个test.cpp文件,键入如下测试代码

#include<iostream>
#include<string>
using namespace std;
int main() {
    string s = "aaa";
    for(int i = 0; i < s.length(); ++i) {
        cout << s[i] << endl;
    }
    system("pause");
    return 0;
}

按下F5,顶部或出现如下菜单,选择C++(GDB/LLDB)

 

系统自动在当前目录下创建.vscode文件夹,并在其中新建一个launch.json的模版文件如下:

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: 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
                }
            ]
        }
    ]
}

将该模版修改为如下(可以直接复制,并修改有注释的一段)

{
    "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 (x86)/Dev-Cpp/MinGW64/bin/gdb.exe", // 这里修改GDB路径为安装的mingw64的bin下的gdb.exe路径
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }]
}

 

vscode中按下组合键Shift+Ctrl+P,在唤出的任务栏中键入>task,下拉找到并点击 Tasks:Configure Task(任务:配置任务)项,并在接下来的返回项中选择使用模版创建tasks.json文件

 

系统会自动在.vscode文件夹下创建一个tasks.json文件,自动生成的代码如下

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "msbuild",
            "args": [
                // Ask msbuild to generate full paths for file names.
                "/property:GenerateFullPaths=true",
                "/t:build"
            ],
            "group": "build",
            "presentation": {
                // Reveal the output only if unrecognized errors occur.
                "reveal": "silent"
            },
            // Use the standard MS compiler pattern to detect errors, warnings and infos
            "problemMatcher": "$msCompile"
        }
    ]
}

同理,将之修改为如下代码(可直接覆盖)


{
    "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++代码页,按下F5,根目录下出现.cpp文件同名的.exe文件,代码自动执行,完成。

注意:每一个代码之后都要加上system("pause");或者getchar();

 

  • 7
    点赞
  • 51
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值