1. 程式人生 > >vscode c++ 編譯生成後,調試時無法命中斷點

vscode c++ 編譯生成後,調試時無法命中斷點

set text return quest class style failure console argc

技術分享
 1 //test.cpp
 2 #include <stdio.h>
 3 int g_var = 0;
 4 void print_line(char *str)
 5 {
 6     if (str != NULL)
 7         printf("%s\r\n", str);
 8     else
 9         printf("null string\r\n");
10 }
11 int main (int argc, char **argv)
12 {
13     int l_var = 1;
14     print_line("hello world!
"); 15 printf("g_var = %d, l_var = %d.\r\n", g_var, l_var); 16 return 0; 17 }
View Code

launch.json

技術分享
 1 {
 2         "version": "0.2.0",
 3         "configurations": [
 4             {
 5                 "name": "(gdb) Launch",
 6                 "type": "cppdbg",
 7                 "request"
: "launch", 8 "program": "${workspaceRoot}/test.exe", 9 "args": [], 10 "stopAtEntry": false, 11 "cwd": "${workspaceRoot}", 12 "environment": [], 13 "externalConsole": true, 14 "MIMode": "gdb
", 15 "miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe", 16 "setupCommands": [ 17 { 18 "description": "Enable pretty-printing for gdb", 19 "text": "-enable-pretty-printing", 20 "ignoreFailures": true 21 } 22 ] 23 } 24 ] 25 }
View Code

tasks.json

技術分享
 1 {
 2     // See https://go.microsoft.com/fwlink/?LinkId=733558
 3     // for the documentation about the tasks.json format
 4     "version": "2.0.0",
 5     "tasks": [
 6         {
 7             "taskName": "test",
 8             "type": "shell",
 9             "command": "g++",
10             "args": ["-g", "${file}", "-o", "${workspaceRoot}/test.exe"]
11         }
12     ]
13 }
View Code

編譯成功後,在源碼中設置斷點,卻無法命中斷點。

後來查看官方c++編譯調試文檔和嘗試,在launch.json文件的

"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]

後面加上

"preLaunchTask": "test" 配置,調試時就可以正常命中斷點了。 註意:別忘了"setupCommands"的中括號’ ] ‘後面加上一個逗號。

vscode c++ 編譯生成後,調試時無法命中斷點