VSCode下建立C++调试环境

最近正在学习C++,最后选择在VS Code配置C++调试环境,在网上参考了很多博客提供的方法,遇到了现有网友遇到的问题,也有一些网上没有出现的新问题。

总结下,希望能帮助到别人。

主要参考这篇博客的方法,里面提供并总结的比较全面。

整理:Visual Studio Code (vscode) 配置C、C++环境/编写运行C、C++(Windows)

https://blog.csdn.net/bat67/article/details/76095813?utm_source=blogxgwz0

但是现在VS Code默认生成的tasks.json 版本是2.0.0的,该博客中提供的是V1.0.0的。 

下面提出我用的launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceRoot}/${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceRoot}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "C:/Program Files/MinGW/bin/gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "g++"
        }
    ]

}

 

tasks.json
{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",    
    "tasks": [        
        {            
            "label":"g++",             
            "type": "shell",            
            "command": "g++",            
            "args": [                
                "-g","${file}","-o","${fileBasenameNoExtension}.exe"            
                ],           
                 "problemMatcher":{                
                     "owner": "cpp",                
                     "fileLocation":["relative","${workspaceRoot}"],                
                     "pattern":{                    
                         "regexp": "^([^\\\\s].*)\\\\((\\\\d+,\\\\d+)\\\\):\\\\s*(.*)$",                    
                         "file": 1,                    
                         "line": 2,                    
                         "column": 3,                    
                         "severity": 4,                    
                         "message":5                
                }            
            }                    
        }   
    ]
}

 

 

配置C++调试环境,实际上是调用C++编译器g++对我们要调试的sourcefile进行编译链接。

这里有几个要注意的地方:

1.首先是需要安装的软件需要全部装好,编译的、调试的、vscode、c++插件等

2.使用的编译器是g++而不是编译c语言的gcc

3.构建的时候需要告诉编译器我要编译哪些文件,路径如何,形成编译命令

 

 

posted on 2018-10-30 16:53  HoKacy  阅读(115)  评论(0)    收藏  举报

导航