Windows下配置VSCode编写C++

本文主要描述如何在Windows下使用MinGW配置VSCode以编写C++程序,如有错误,还望评论指正。
参考链接:官方教程feynman1999的文章

安装

下载并安装VSCode;

下载MinGW-w64,当然你也可以下载其他版本的MinGW;

打开VSCode,在扩展中搜索C++,安装官方(Microsoft)发布的C/C++扩展,之后重新加载窗口;

配置

打开一个文件夹,路径不要包含中文。类似于Codeblocks或者其他重量级的IDE,VSCode虽然不需要(也不能)创建工程,但需要在一个文件夹下才能编译、调试C++代码。;

打开菜单栏 Terminal - Configure Default Build Tasks… 或使用快捷键Ctrl +shift + B选择create task.json,选择others,即会生成默认代码,将其修改为:

tasks.json
{
    "tasks": [
        {
            "label": "compile",  // 任务标识,下面launch.json里面会用到
            "type": "shell",
            "command": "g++",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileBasenameNoExtension}.exe",
                "-ggdb3",
                "-Wall",
                "-static-libgcc",
                "-std=c++14",
                "-Wno-format",
                "-finput-charset=UTF-8",
                "-fexec-charset=UTF-8"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

打开菜单栏 Debug - Open Configuration 或者直接按F5,选择 C++(GDB/LLDB) 生成launch.json,将其修改为:

launch.json
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}.exe", // 可执行文件
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false, // 是否使用外部控制台窗口
            "MIMode": "gdb",
            "miDebuggerPath": "D:/mingw64/bin/gdb.exe", // 你的gdb位置
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "compile"  // 即是否在调试前编译,compile与task.josn中的label值一样
        }
    ]
}

选择菜单栏 View - Command Palette 或使用快捷键Ctrl + shift + P打开命令面板,输入并选择C/Cpp的edit configuration,生成c_cpp_properties.json文件,修改内容为:

c_cpp_properties.json
{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [ // include路径,可以手动添加
                "${workspaceFolder}/**",
                "D:\\mingw64\\lib\\gcc\\x86_64-w64-mingw32\\8.1.0\\include/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE"
            ],
            "compilerPath": "C:\\mingw64\\bin\\g++.exe", // 编译器路径
            "cStandard": "c11",
            "cppStandard": "c++14",
            "intelliSenseMode": "clang-x64",
            "browse": {
                "path": [
                    "${workspaceFolder}" 
                ],
                "limitSymbolsToIncludedHeaders": true,
                "databaseFilename": ""
            }
        }
    ],
    "version": 4
}

上述三个文件配置完之后,就可以编译运行编写的C++代码了,文件架结构:

E:.
│  data.out
│  test.cpp
│  test.exe
│
└─.vscode
        c_cpp_properties.json
        launch.json
        settings.json
        tasks.json

问题小计:

  • launch:program test.exe not exist :launch.json里面可执行文件路径未设置好,${fileDirname}这类替换变量如果拼写错误或者路径文件夹缺少就容易出现这种问题。
  • **调试器意外终止 Debug adapter process has terminated unexpectedly (read error) **: 就我遇到的问题而言,Windows下,错误原因也是launch.json出现了拼写错误。Ubuntu环境下将externalConsole一项设置为 false后就能正常调试了。参考:官方issues,可能会在下个版本中修复。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值