新聞中心
最近開始在ubuntu下使用Vs Codel,真的方便,可以和git結(jié)合。下面總結(jié)一下如何調(diào)試程序,

創(chuàng)新互聯(lián)公司10多年成都定制網(wǎng)頁設(shè)計服務(wù);為您提供網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)頁設(shè)計及高端網(wǎng)站定制服務(wù),成都定制網(wǎng)頁設(shè)計及推廣,對成都搬家公司等多個方面擁有多年的網(wǎng)站維護(hù)經(jīng)驗的網(wǎng)站建設(shè)公司。
我寫了一個實(shí)例程序(不重要)
#include
#include
#include
#include
#include
using namespace std;
int main(void)
{
fstream iofile("test.txt");
vector
int i=0;
if(!iofile){
cerr<<"open file failed"<
while(iofile>>strs[i++]);
}
for(int j=0;j
cout<
// cout<
cout<<"i:"< for(int j=0;j {
cout<
cout<
}
這個時候,我們按F5,發(fā)現(xiàn)不能運(yùn)行,它提示需要一個Launch.json文件,OK,這是一個啟動文件,我們來配置它。
{
// 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": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build",
"preLaunchTask": "build",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
注意,這里需要修改的部分主要是program那一行,僅需修改為自己編譯后產(chǎn)生的文件名,不如g++ -g 1.7.cpp -o build,所以這里我就取了build這個名字。
還有,就是要添加preLaunchTask這一行,名字與下面的task要對應(yīng)。
這里,它還需要一個task.json文件,配置如下,
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "g++",
"args": [
"-g",
"${workspaceFolder}/chapter01/1.7.cpp",
"-o",
"build"
]
}
]
}
大家可以看到,這里只需要將label的值要與上面的preLaunchTask相對應(yīng),其他的就是命令行部分,完成這個文件后,我們ctrl+shift+b,這是會執(zhí)行task。
之后,我們打開main文件,設(shè)置斷點(diǎn),F(xiàn)5即可開始調(diào)試代碼。
關(guān)于Launch.json:
官方是這樣說的:However, for most debugging scenarios, creating a launch configuration file is beneficial because it allows you to configure and save debugging setup details. VS Code keeps debugging configuration information in a launch.json file located in a .vscode folder in your workspace (project root folder) or in your user settings or workspace settings.但是,對于大多數(shù)調(diào)試方案,創(chuàng)建啟動配置文件是有益的,因為它允許您配置和保存調(diào)試設(shè)置詳細(xì)信息。 VS Code將配置信息保存在位于工作區(qū)(項目根文件夾)的.vscode文件夾或用戶設(shè)置或工作區(qū)設(shè)置中的launch.json文件中。
個人感覺可能是項目比較簡單所以看不來它的好處。
本文標(biāo)題:Ubuntu下VSCode如何調(diào)試C++代碼
分享鏈接:http://fisionsoft.com.cn/article/djhisio.html


咨詢
建站咨詢
