mac上在vsCode上進行c/c++程序的調(diào)試
目的在mac上使用vscode 和 lldg/gdb調(diào)試工具 對編寫c/c++程序進行調(diào)試。
調(diào)試下面編寫的c++代碼,文件名稱main.cpp,循環(huán)中的輸出語句中設(shè)置斷點進行調(diào)試。
#include<iostream>
int main(){
int i = 0;
for(i = 0;i<10;++i){
std::cout<<i<<std::endl;
}
std::cout<<"done!\n";
return 0;
}
二個需要的文件(一個負(fù)責(zé)編譯 一個負(fù)責(zé)調(diào)試)
在使用vscode進行c++調(diào)試最重要的是需要launch.json文件(負(fù)責(zé)調(diào)試程序),但是經(jīng)常也需要tasks.json文件(負(fù)責(zé)編譯程序).
下面簡單介紹一個對應(yīng)文件如何編寫和使用.
1. tasks.json
生成命令:(shift+command+p -> Tasks:Configure Tasks, -> Create tasks.json form templates -> Others )
主要是對之后寫的代碼進行編譯,而該編譯操作命令寫成一個任務(wù),使用vscode的task用來編譯自己寫的代碼。
默認(rèn)形成的任務(wù)(task),該任務(wù)的作用從shell上,執(zhí)行echo hello 命令。
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "echo",
"type": "shell",
"command": "echo Hello"
}
]
}
但是默認(rèn)的任務(wù)是不能使用(shift+command+B)執(zhí)行的(會提示沒有No build task to run ),沒有build的任務(wù)去執(zhí)行。 需要在該文件中添加以下內(nèi)容:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "echo",
"type": "shell",
"command": "echo Hello",
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
此時就可以使用(shift+command+B)執(zhí)行默認(rèn)任務(wù)了。
當(dāng)想建立一個執(zhí)行編譯c++程序的任務(wù)基本模塊表示為:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build main",
"type": "shell",
"command": "clang++",
"args": [
"main.cpp",
"-o",
a.out",
"-g"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
參數(shù)說明:
"label": 任務(wù)的名稱 (build main)
"type" : 任務(wù)的類型,一共有兩種(shell/Process),其中shell表示先打開shell,再執(zhí)行輸入命令;process則直接執(zhí)行命令 (由于編譯c++ 需要借助shell上進行執(zhí)行命令)
"command": 實際上執(zhí)行的命令(c++ 使用clang++命令進行編譯)
"args": 需要設(shè)置的一些參數(shù),應(yīng)該是跟再command命令后面的.(此處表示對main.cpp文件進行使用clang++編譯,其中參數(shù)表示的命令: clang++ main.cpp -o a.out -g ; 當(dāng)使用-g 表示c/c++調(diào)試必備的一些參數(shù) 同時會再文件目錄生成一個可執(zhí)行文件名開頭,DSYM結(jié)尾的文件夾(a.out.DSYM)對應(yīng)的文件夾)
詳細(xì)參考:
VScode官方文檔關(guān)于tasks.json的說明
Github上tasks.json的模板
2. launch.json
(在dubug上,找到設(shè)置,新建一個c++(GDB/LLDB))生成一個對應(yīng)的文件
作用啟動的命令,用來執(zhí)行啟動調(diào)試的。
默認(rèn)生成的文件
{
// 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": "(lldb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "enter program name, for example ${workspaceFolder}/a.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "lldb"
}
]
}
為了調(diào)試自己的任務(wù)只需修改paragram對應(yīng)的參數(shù)即可。
{
// 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": "(lldb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/a.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "lldb"
}
]
}
參數(shù)說明
"name": 配置名稱,之后會出現(xiàn)再調(diào)試窗口的啟動配置上
"type": 配置類型(不知道是否可以修改TODO:)
"request": 請求配置類型,可以設(shè)置為 launch(啟動) 或者 attach(附加)
"program": 進行調(diào)試的程序的位置(此處在當(dāng)前文件夾下的a.out可執(zhí)行文件)
"stopAtEntry": 設(shè)置為true時,程序?qū)和T俪绦虻娜肟谥?br>
"cmd": 當(dāng)前調(diào)試所在的路徑
"externalConsole": 調(diào)試是否顯示控制臺窗口,true即顯示控制臺
詳細(xì)說明:
Github中關(guān)于launch.json文件的說明
主要一定要在編譯過程中添加-g命令,不然程序無法進行調(diào)試.