tft每日頭條

 > 知識

 > vscode怎麼調試c語言

vscode怎麼調試c語言

知識 更新时间:2025-02-06 17:45:32

  1、打開你要寫c++程序的文件夾,我們這裡新建一個Test文件夾并打開test,打開後:使用VScode運行調試C/C++,在左側打開的目錄中新建一個 main.cpp 文件。

  2、新建後點左側的調試按鈕(英文:Debug),可以看到,目前沒有調試配置。

  3、這時我們需要配置自己的調試配置,回到資源管理器界面,我們可以看到目錄下多了一個.vscode的文件夾,裡面有一個launch.json文件。我們現在在這個文件夾中新建一個tasks.json文件。我們需要改寫這兩個json文件的内容。

  {

  version: 0.2.0,

  configurations: [

  {

  name: Run C/C++,

  type: cppdbg,

  request: launch,

  program: ${workspaceFolder}/${fileBasenameNoExtension}.exe,

  args: [],

  stopAtEntry: false,

  cwd: ${workspaceFolder},

  environment: [],

  externalConsole: true,

  MIMode: gdb,

  miDebuggerPath: C:/Program Files (x86)/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/bin/gdb.exe,

  setupCommands: [

  {

  description: Enable pretty-printing for gdb,

  text: -enable-pretty-printing,

  ignoreFailures: false

  }

  ],

  preLaunchTask: build & run file

  },

  {

  name: Debug C/C++,

  type: cppdbg,

  request: launch,

  program: ${workspaceFolder}/${fileBasenameNoExtension}.exe,

  args: [],

  stopAtEntry: false,

  cwd: ${workspaceFolder},

  environment: [],

  externalConsole: true,

  MIMode: gdb,

  miDebuggerPath: C:/Program Files (x86)/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/bin/gdb.exe,

  setupCommands: [

  {

  description: Enable pretty-printing for gdb,

  text: -enable-pretty-printing,

  ignoreFailures: false

  }

  ],

  preLaunchTask: build & debug file

  }

  ]

  }

  其中tasks.json文件内容(直接複制即可):

  {

  version: 2.0.0,

  tasks: [

  {

  label: build & debug file,

  type: shell,

  command: g++,

  args: [

  -g,

  -o,

  ${fileBasenameNoExtension},

  ${file}

  ],

  group: {

  kind: build,

  isDefault: true

  }

  },

  {

  label: build & run file,

  type: shell,

  command: g++,

  args: [

  -o,

  ${fileBasenameNoExtension},

  ${file}

  ],

  group: {

  kind: build,

  isDefault: true

  }

  }

  ]

  }

  4、我們已經完成了基本的配置工作,保存以上兩個json文件後,再次點擊左側的調試按鈕(Debug),可以發現出現了兩個新的配置,一個是直接運行程序的Run,一個是用來調試程序的Debug。

  5、我們寫一個簡單的c++程序來進行調試說明,程序源代碼,我們在return 0這行添加了一個斷點。

  6、切換運行配置為Debug C/C++,點擊運行按鈕開始調試。

  7、可以看到,彈出的終端輸出了正确的結果,調試窗口中的變量中也有變量a和對應的值。

更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!

查看全部

相关知識资讯推荐

热门知識资讯推荐

网友关注

Copyright 2023-2025 - www.tftnews.com All Rights Reserved