本篇博客以一个简单的hello world程序,介绍在vscode中调试C++代码的配置过程。linux
vscode是一个轻量的代码编辑器,并不具有代码编译功能,代码编译须要交给编译器完成。linux下最经常使用的编译器是gcc,经过以下命令安装:c++
sudo apt-get install build-essential
安装成功以后,在终端中执行gcc --version
或者g++ --version
,能够看到编译器的版本信息,说明安装成功。shell
在vscode中编写C++代码,C/C++插件是必不可少的。打开vscode,点击左边侧边栏最下面的正方形图标,在搜索框里输入c++
,安装插件。
json
hello world程序,略。编辑器
在task里添加编译命令,从而执行编译操做。步骤以下:ui
ctrl+shift+P
,打开命令面板;{ // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format "version": "2.0.0", "tasks": [ { "label": "build hello world", // task的名字 "type": "shell", "command": "g++", //编译命令 "args": [ //编译参数列表 "main.cpp", "-o", "main.out" ] } ] }
上面的command
是咱们的编译命令,args
是编译参数列表,合在一块儿,其实就是咱们手动编译时的命令。spa
g++ main.cpp -o main.out
把debug的内容配置在launch.json,这样咱们就能够使用断点调试了。插件
{ // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "debug hello world", //名称 "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/main.out", //当前目录下编译后的可执行文件 "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", //表示当前目录 "environment": [], "externalConsole": false, // 在vscode自带的终端中运行,不打开外部终端 "MIMode": "gdb", //用gdb来debug "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "build hello world" //在执行debug hello world前,先执行build hello world这个task,看第4节 } ] }
至此,配置完成,按F5能够编译和调试代码,vscode自带终端中会打印hello world字符串。在程序中添加断点,调试时也会在断点处中断。debug