Cách gỡ lỗi trong mã phòng thu trực quan c ++
Trong bài viết này, chúng ta sẽ thảo luận về thiết lập Mã VS cần thiết để gỡ lỗi điểm ngắt. Đầu tiên tạo một tập tin khởi chạy. json định cấu hình Mã VS để khởi chạy trình gỡ lỗi GDB khi bắt đầu quá trình gỡ lỗi. Sau đó tạo một nhiệm vụ tập tin. json cho VS Code biết cách xây dựng (biên dịch) chương trình. Cuối cùng, thực hiện một số thay đổi đối với cài đặt bảng điều khiển và triển khai quá trình xây dựng và gỡ lỗi Chương trình. C++{ "version": "2.0.0", "tasks": [ { "type": "shell", "label": "C/C++: g++.exe build active file", "command": "C:\\MinGW\\bin\\g++.exe", "args": [ "-std=c++11", "-O2", "-Wall", "-g", "${file}", "-o", "${fileDirname} \\ ${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${workspaceFolder}" }, "problemMatcher": [ "$gcc" ], "group": { "isDefault": true, "kind": "build" } } ] }9 { "version": "2.0.0", "tasks": [ { "type": "shell", "label": "C/C++: g++.exe build active file", "command": "C:\\MinGW\\bin\\g++.exe", "args": [ "-std=c++11", "-O2", "-Wall", "-g", "${file}", "-o", "${fileDirname} \\ ${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${workspaceFolder}" }, "problemMatcher": [ "$gcc" ], "group": { "isDefault": true, "kind": "build" } } ] }0 { "version": "2.0.0", "tasks": [ { "type": "shell", "label": "C/C++: g++.exe build active file", "command": "C:\\MinGW\\bin\\g++.exe", "args": [ "-std=c++11", "-O2", "-Wall", "-g", "${file}", "-o", "${fileDirname} \\ ${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${workspaceFolder}" }, "problemMatcher": [ "$gcc" ], "group": { "isDefault": true, "kind": "build" } } ] }1 { "version": "2.0.0", "tasks": [ { "type": "shell", "label": "C/C++: g++.exe build active file", "command": "C:\\MinGW\\bin\\g++.exe", "args": [ "-std=c++11", "-O2", "-Wall", "-g", "${file}", "-o", "${fileDirname} \\ ${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${workspaceFolder}" }, "problemMatcher": [ "$gcc" ], "group": { "isDefault": true, "kind": "build" } } ] }2 { "version": "2.0.0", "tasks": [ { "type": "shell", "label": "C/C++: g++.exe build active file", "command": "C:\\MinGW\\bin\\g++.exe", "args": [ "-std=c++11", "-O2", "-Wall", "-g", "${file}", "-o", "${fileDirname} \\ ${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${workspaceFolder}" }, "problemMatcher": [ "$gcc" ], "group": { "isDefault": true, "kind": "build" } } ] }3 { "version": "2.0.0", "tasks": [ { "type": "shell", "label": "C/C++: g++.exe build active file", "command": "C:\\MinGW\\bin\\g++.exe", "args": [ "-std=c++11", "-O2", "-Wall", "-g", "${file}", "-o", "${fileDirname} \\ ${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${workspaceFolder}" }, "problemMatcher": [ "$gcc" ], "group": { "isDefault": true, "kind": "build" } } ] }4
{ "version": "2.0.0", "tasks": [ { "type": "shell", "label": "C/C++: g++.exe build active file", "command": "C:\\MinGW\\bin\\g++.exe", "args": [ "-std=c++11", "-O2", "-Wall", "-g", "${file}", "-o", "${fileDirname} \\ ${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${workspaceFolder}" }, "problemMatcher": [ "$gcc" ], "group": { "isDefault": true, "kind": "build" } } ] }5 { "version": "2.0.0", "tasks": [ { "type": "shell", "label": "C/C++: g++.exe build active file", "command": "C:\\MinGW\\bin\\g++.exe", "args": [ "-std=c++11", "-O2", "-Wall", "-g", "${file}", "-o", "${fileDirname} \\ ${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${workspaceFolder}" }, "problemMatcher": [ "$gcc" ], "group": { "isDefault": true, "kind": "build" } } ] }6 { "version": "2.0.0", "tasks": [ { "type": "shell", "label": "C/C++: g++.exe build active file", "command": "C:\\MinGW\\bin\\g++.exe", "args": [ "-std=c++11", "-O2", "-Wall", "-g", "${file}", "-o", "${fileDirname} \\ ${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${workspaceFolder}" }, "problemMatcher": [ "$gcc" ], "group": { "isDefault": true, "kind": "build" } } ] }0 { "version": "2.0.0", "tasks": [ { "type": "shell", "label": "C/C++: g++.exe build active file", "command": "C:\\MinGW\\bin\\g++.exe", "args": [ "-std=c++11", "-O2", "-Wall", "-g", "${file}", "-o", "${fileDirname} \\ ${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${workspaceFolder}" }, "problemMatcher": [ "$gcc" ], "group": { "isDefault": true, "kind": "build" } } ] }1 { "version": "2.0.0", "tasks": [ { "type": "shell", "label": "C/C++: g++.exe build active file", "command": "C:\\MinGW\\bin\\g++.exe", "args": [ "-std=c++11", "-O2", "-Wall", "-g", "${file}", "-o", "${fileDirname} \\ ${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${workspaceFolder}" }, "problemMatcher": [ "$gcc" ], "group": { "isDefault": true, "kind": "build" } } ] }2 { "version": "2.0.0", "tasks": [ { "type": "shell", "label": "C/C++: g++.exe build active file", "command": "C:\\MinGW\\bin\\g++.exe", "args": [ "-std=c++11", "-O2", "-Wall", "-g", "${file}", "-o", "${fileDirname} \\ ${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${workspaceFolder}" }, "problemMatcher": [ "$gcc" ], "group": { "isDefault": true, "kind": "build" } } ] }6 { "version": "2.0.0", "tasks": [ { "type": "shell", "label": "C/C++: g++.exe build active file", "command": "C:\\MinGW\\bin\\g++.exe", "args": [ "-std=c++11", "-O2", "-Wall", "-g", "${file}", "-o", "${fileDirname} \\ ${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${workspaceFolder}" }, "problemMatcher": [ "$gcc" ], "group": { "isDefault": true, "kind": "build" } } ] }4 { "version": "2.0.0", "tasks": [ { "type": "shell", "label": "C/C++: g++.exe build active file", "command": "C:\\MinGW\\bin\\g++.exe", "args": [ "-std=c++11", "-O2", "-Wall", "-g", "${file}", "-o", "${fileDirname} \\ ${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${workspaceFolder}" }, "problemMatcher": [ "$gcc" ], "group": { "isDefault": true, "kind": "build" } } ] }5 { "version": "2.0.0", "tasks": [ { "type": "shell", "label": "C/C++: g++.exe build active file", "command": "C:\\MinGW\\bin\\g++.exe", "args": [ "-std=c++11", "-O2", "-Wall", "-g", "${file}", "-o", "${fileDirname} \\ ${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${workspaceFolder}" }, "problemMatcher": [ "$gcc" ], "group": { "isDefault": true, "kind": "build" } } ] }6
{ "version": "2.0.0", "tasks": [ { "type": "shell", "label": "C/C++: g++.exe build active file", "command": "C:\\MinGW\\bin\\g++.exe", "args": [ "-std=c++11", "-O2", "-Wall", "-g", "${file}", "-o", "${fileDirname} \\ ${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${workspaceFolder}" }, "problemMatcher": [ "$gcc" ], "group": { "isDefault": true, "kind": "build" } } ] }2____08 _______02____190
{ "version": "2.0.0", "tasks": [ { "type": "shell", "label": "C/C++: g++.exe build active file", "command": "C:\\MinGW\\bin\\g++.exe", "args": [ "-std=c++11", "-O2", "-Wall", "-g", "${file}", "-o", "${fileDirname} \\ ${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${workspaceFolder}" }, "problemMatcher": [ "$gcc" ], "group": { "isDefault": true, "kind": "build" } } ] }2 { "version": "2.0.0", "tasks": [ { "type": "shell", "label": "C/C++: g++.exe build active file", "command": "C:\\MinGW\\bin\\g++.exe", "args": [ "-std=c++11", "-O2", "-Wall", "-g", "${file}", "-o", "${fileDirname} \\ ${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${workspaceFolder}" }, "problemMatcher": [ "$gcc" ], "group": { "isDefault": true, "kind": "build" } } ] }92 { "version": "2.0.0", "tasks": [ { "type": "shell", "label": "C/C++: g++.exe build active file", "command": "C:\\MinGW\\bin\\g++.exe", "args": [ "-std=c++11", "-O2", "-Wall", "-g", "${file}", "-o", "${fileDirname} \\ ${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${workspaceFolder}" }, "problemMatcher": [ "$gcc" ], "group": { "isDefault": true, "kind": "build" } } ] }2 { "version": "2.0.0", "tasks": [ { "type": "shell", "label": "C/C++: g++.exe build active file", "command": "C:\\MinGW\\bin\\g++.exe", "args": [ "-std=c++11", "-O2", "-Wall", "-g", "${file}", "-o", "${fileDirname} \\ ${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${workspaceFolder}" }, "problemMatcher": [ "$gcc" ], "group": { "isDefault": true, "kind": "build" } } ] }94 { "version": "2.0.0", "tasks": [ { "type": "shell", "label": "C/C++: g++.exe build active file", "command": "C:\\MinGW\\bin\\g++.exe", "args": [ "-std=c++11", "-O2", "-Wall", "-g", "${file}", "-o", "${fileDirname} \\ ${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${workspaceFolder}" }, "problemMatcher": [ "$gcc" ], "group": { "isDefault": true, "kind": "build" } } ] }95 { "version": "2.0.0", "tasks": [ { "type": "shell", "label": "C/C++: g++.exe build active file", "command": "C:\\MinGW\\bin\\g++.exe", "args": [ "-std=c++11", "-O2", "-Wall", "-g", "${file}", "-o", "${fileDirname} \\ ${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${workspaceFolder}" }, "problemMatcher": [ "$gcc" ], "group": { "isDefault": true, "kind": "build" } } ] }6 { "version": "2.0.0", "tasks": [ { "type": "shell", "label": "C/C++: g++.exe build active file", "command": "C:\\MinGW\\bin\\g++.exe", "args": [ "-std=c++11", "-O2", "-Wall", "-g", "${file}", "-o", "${fileDirname} \\ ${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${workspaceFolder}" }, "problemMatcher": [ "$gcc" ], "group": { "isDefault": true, "kind": "build" } } ] }97 { "version": "2.0.0", "tasks": [ { "type": "shell", "label": "C/C++: g++.exe build active file", "command": "C:\\MinGW\\bin\\g++.exe", "args": [ "-std=c++11", "-O2", "-Wall", "-g", "${file}", "-o", "${fileDirname} \\ ${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${workspaceFolder}" }, "problemMatcher": [ "$gcc" ], "group": { "isDefault": true, "kind": "build" } } ] }98 { "version": "2.0.0", "tasks": [ { "type": "shell", "label": "C/C++: g++.exe build active file", "command": "C:\\MinGW\\bin\\g++.exe", "args": [ "-std=c++11", "-O2", "-Wall", "-g", "${file}", "-o", "${fileDirname} \\ ${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${workspaceFolder}" }, "problemMatcher": [ "$gcc" ], "group": { "isDefault": true, "kind": "build" } } ] }5 { "version": "2.0.0", "tasks": [ { "type": "shell", "label": "C/C++: g++.exe build active file", "command": "C:\\MinGW\\bin\\g++.exe", "args": [ "-std=c++11", "-O2", "-Wall", "-g", "${file}", "-o", "${fileDirname} \\ ${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${workspaceFolder}" }, "problemMatcher": [ "$gcc" ], "group": { "isDefault": true, "kind": "build" } } ] }00 { "version": "2.0.0", "tasks": [ { "type": "shell", "label": "C/C++: g++.exe build active file", "command": "C:\\MinGW\\bin\\g++.exe", "args": [ "-std=c++11", "-O2", "-Wall", "-g", "${file}", "-o", "${fileDirname} \\ ${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${workspaceFolder}" }, "problemMatcher": [ "$gcc" ], "group": { "isDefault": true, "kind": "build" } } ] }5 { "version": "2.0.0", "tasks": [ { "type": "shell", "label": "C/C++: g++.exe build active file", "command": "C:\\MinGW\\bin\\g++.exe", "args": [ "-std=c++11", "-O2", "-Wall", "-g", "${file}", "-o", "${fileDirname} \\ ${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${workspaceFolder}" }, "problemMatcher": [ "$gcc" ], "group": { "isDefault": true, "kind": "build" } } ] }02 { "version": "2.0.0", "tasks": [ { "type": "shell", "label": "C/C++: g++.exe build active file", "command": "C:\\MinGW\\bin\\g++.exe", "args": [ "-std=c++11", "-O2", "-Wall", "-g", "${file}", "-o", "${fileDirname} \\ ${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${workspaceFolder}" }, "problemMatcher": [ "$gcc" ], "group": { "isDefault": true, "kind": "build" } } ] }2____204
{ "version": "2.0.0", "tasks": [ { "type": "shell", "label": "C/C++: g++.exe build active file", "command": "C:\\MinGW\\bin\\g++.exe", "args": [ "-std=c++11", "-O2", "-Wall", "-g", "${file}", "-o", "${fileDirname} \\ ${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${workspaceFolder}" }, "problemMatcher": [ "$gcc" ], "group": { "isDefault": true, "kind": "build" } } ] }2____206 { "version": "2.0.0", "tasks": [ { "type": "shell", "label": "C/C++: g++.exe build active file", "command": "C:\\MinGW\\bin\\g++.exe", "args": [ "-std=c++11", "-O2", "-Wall", "-g", "${file}", "-o", "${fileDirname} \\ ${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${workspaceFolder}" }, "problemMatcher": [ "$gcc" ], "group": { "isDefault": true, "kind": "build" } } ] }2____208 { "version": "2.0.0", "tasks": [ { "type": "shell", "label": "C/C++: g++.exe build active file", "command": "C:\\MinGW\\bin\\g++.exe", "args": [ "-std=c++11", "-O2", "-Wall", "-g", "${file}", "-o", "${fileDirname} \\ ${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${workspaceFolder}" }, "problemMatcher": [ "$gcc" ], "group": { "isDefault": true, "kind": "build" } } ] }5 { "version": "2.0.0", "tasks": [ { "type": "shell", "label": "C/C++: g++.exe build active file", "command": "C:\\MinGW\\bin\\g++.exe", "args": [ "-std=c++11", "-O2", "-Wall", "-g", "${file}", "-o", "${fileDirname} \\ ${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${workspaceFolder}" }, "problemMatcher": [ "$gcc" ], "group": { "isDefault": true, "kind": "build" } } ] }10 { "version": "2.0.0", "tasks": [ { "type": "shell", "label": "C/C++: g++.exe build active file", "command": "C:\\MinGW\\bin\\g++.exe", "args": [ "-std=c++11", "-O2", "-Wall", "-g", "${file}", "-o", "${fileDirname} \\ ${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${workspaceFolder}" }, "problemMatcher": [ "$gcc" ], "group": { "isDefault": true, "kind": "build" } } ] }04 Phóng. jsonTệp này liên quan đến thông tin như tên của trình gỡ lỗi, đường dẫn đến trình gỡ lỗi, thư mục của tệp CPP hiện tại và bảng điều khiển liên quan đến dữ liệu. Dưới đây là mã trong tệp khởi chạy. json { "version": "2.0.0", "tasks": [ { "type": "shell", "label": "C/C++: g++.exe build active file", "command": "C:\\MinGW\\bin\\g++.exe", "args": [ "-std=c++11", "-O2", "-Wall", "-g", "${file}", "-o", "${fileDirname} \\ ${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${workspaceFolder}" }, "problemMatcher": [ "$gcc" ], "group": { "isDefault": true, "kind": "build" } } ] }7 Các điều khoản quan trọng khi ra mắt. json. Bạn có thể sử dụng hoàn toàn mã được cung cấp ở trên hoặc nếu không, bạn có thể sử dụng mã được tạo tự động trong máy của mình. Trong cả hai trường hợp, bạn phải thực hiện một số biện pháp phòng ngừa. Đầu tiên, đảm bảo rằng thuộc tính “externalConsole” được đánh dấu là đúng (chúng ta sẽ hiểu ý nghĩa của nó sau trong chủ đề này). Sau đó kiểm tra xem thuộc tính “miDebuggerPath” có trỏ đến trình gỡ lỗi gdb chính xác không. Hãy để chúng tôi thảo luận chi tiết về các thuộc tính này
bước
nhiệm vụ. jsonTệp này chứa thông tin như các lệnh được sử dụng để biên dịch, địa chỉ của trình biên dịch, nhãn giống như khi khởi chạy. json và một số thông tin khác. Dưới đây là mã trong tác vụ tệp. json { "version": "2.0.0", "tasks": [ { "type": "shell", "label": "C/C++: g++.exe build active file", "command": "C:\\MinGW\\bin\\g++.exe", "args": [ "-std=c++11", "-O2", "-Wall", "-g", "${file}", "-o", "${fileDirname} \\ ${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${workspaceFolder}" }, "problemMatcher": [ "$gcc" ], "group": { "isDefault": true, "kind": "build" } } ] } Các thuật ngữ quan trọng trong nhiệm vụ. json. Các nhiệm vụ nêu trên. json có thể được sử dụng hoàn toàn mà không cần thận trọng. Tuy nhiên, điều duy nhất cần quan tâm là nhãn của nhiệm vụ. json phải khớp với preLaunchTask của Launch. json. Hãy thảo luận chi tiết hơn về một số thuật ngữ Có trình sửa lỗi cho C không?Thực thi chương trình C trong trình gỡ lỗi gdb
. Bạn cũng có thể đưa ra các đối số dòng lệnh cho chương trình thông qua run args. Chương trình ví dụ mà chúng tôi sử dụng ở đây không yêu cầu bất kỳ đối số dòng lệnh nào, vì vậy hãy để chúng tôi chạy và bắt đầu thực hiện chương trình.
Visual Studio Code có tốt cho C không?Với việc bổ sung các tiện ích mở rộng, VSCode là một công cụ tuyệt vời để nhanh chóng truy cập và chỉnh sửa mã C++ , cho dù bạn là người mới bắt đầu hay người có trình độ cao . Với việc bổ sung một trình biên dịch mã nguồn mở dành riêng cho nền tảng của bạn, bạn có thể sử dụng VSCode như một công cụ hoàn chỉnh để xây dựng các giải pháp C++ của mình. |