使用Visual Studio Code开发(编译、调试)C++程序

总体安装步骤

  1. 安装VSC(Visual Studio Code)
  2. 安装C/C++编译器(如MinGW-w64),然后配置好环境变量。//完成这步即可在VSC的终端(命令行)下编译、运行.cpp程序了。
    • 配置Path变量,加入mingw的安装路径,如d:/mingw64/bin/
  3. 安装并配置Code Runner插件,一键编译运行。
    • 打开扩展(文件-首选项-扩展,或者ctrl+shift+x),搜索Code Runner。
    • 为了在控制台输入,需要配置:"文件-首选项-设置-用户设置-扩展-Run Code Configuration",勾选Run In Terminal
  4. 安装并配置Intellisense。请参见Visual Studio Code include file not found in include directory
    • 一般如果你新建的是.cpp,VSC会自动提示你安装相应插件。
  5. 配置编译环境
    • 需配置launch.json与task.json

task.json(用于build)

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build hello",
            "type": "shell",
            "command": "g++",
            "args": [
                "-g","${file}","-o","${fileDirname}/${fileBasenameNoExtension}.exe"  //代表build的是当前文件
            ],
            "group": {                   //该配置:按ctrl+shift+b就可以直接build当前文件
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

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": true,
            "MIMode": "gdb",
            "miDebuggerPath": "d:/mingw64/bin/gdb.exe",  //mingw的调试程序所在路径
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true                    
                }
            ],
            "preLaunchTask": "build hello" //要与tasks.json中的label属性值一样
        }
    ]
}

参考资料

windows下使用vscode编写运行以及调试C/C++
C/C++ for Visual Studio Code (Preview)

猜你喜欢

转载自www.cnblogs.com/zhrb/p/10538690.html