windows下使用vsc编译调试c++程序

vsc是一个非常轻量级的编辑器,当我们编写c++程序的时候,如果不是编写大的工程项目,而只是自己学习c++或者算法,使用vsc是非常方便的。
在windows平台下,配合MinGW编译器,编写和调试小型cpp非常方便,使用方法如下。
1,windows下安装MinGW-w64编译器
MinGW-w64
下载直接解压
2,vsc安装插件:
Code runner
Clang
3,设置MinGW环境变量 bin路径
4,设置vsc项目,在.vscode文件夹下

  • 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": "E:/Program Files/mingw64/bin/gdb.exe",
            "preLaunchTask": "g++",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
        },
    ]
}
  • tasks.json
{
    "version": "0.1.0",
    "command": "g++",
    "args": ["-g","${file}","-o","${fileDirname}/${fileBasenameNoExtension}.exe"], 
    "problemMatcher": {
        "owner": "cpp",
        "fileLocation": ["relative", "${workspaceRoot}"],
        "pattern": {
            "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
            "file": 1,
            "line": 2,
            "column": 3,
            "severity": 4,
            "message": 5
        }
    }
}
  • c_cpp_properties.json文件,includePath添加include路径
 "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "${vcpkgRoot}/x64-windows/include",
                "${vcpkgRoot}/x86-windows/include",
                "E:/Program Files/mingw64/include"
            ],

参考文章:
1,https://www.cnblogs.com/TAMING/p/8560253.html
2,https://www.cnblogs.com/xiongxx/p/6239213.html
3,https://blog.csdn.net/bat67/article/details/76095813
4,https://blog.csdn.net/qq_34347375/article/details/81137962

你可能感兴趣的:(c++)