常使用visual studio code(vs code)打開.c文件,如果讓vs code具備調試技能估計會比較有用
准備工作:
1. vs code安裝插件:cpptools
2. windows安裝MinGW,然后配置MinGW,需要的工具有gcc,g++,gdb,最后將MinGW的路徑添加到path系統環境變量
寫個hello world測試一下(首先需要打開文件夾):
1. 源程序:test.c
#include <stdio.h> #include <stdlib.h> int main(void) { int a = 1; int b; printf("Hello vs code!\n"); b = a; printf("b = %d\n", b); system("pause"); return 0; }
2. 配置tasks.json文件
使用ctrl+shift+p調出命令對話框,輸入:task,選擇:configure task runner,繼續選擇:others,即可產生tasks.json文件,改為如下內容:
1 { 2 // See https://go.microsoft.com/fwlink/?LinkId=733558 3 // for the documentation about the tasks.json format 4 "version": "0.1.0", 5 "command": "gcc", 6 "isShellCommand": true, 7 "args": ["-g", "${file}", "-o", "${file}.exe"], 8 "showOutput": "always" 9 }
第5行:命令為gcc
第7行:命令gcc的輸入參數,-g表示輸出調試信息,-o后接輸出文件
這個task即是用gcc編譯源程序了:gcc -g test.c -o test.c.exe
到這里就可以使用ctrl+shift+b來build源程序了,切換到test.c頁面下,使用快捷鍵即可編譯出test.c.exe文件
順便打開下方的“終端”選項卡,輸入“.\test.c.exe”來運行
3. 配置launch.json文件
切換到test.c頁面,按下F5,在“選擇環境”對話框中輸入GDB,即選擇“C++ (GDB/LLDB)”,產生launch.json文件
將“program”的值改為:
"program": "${file}.exe",
順便在下面加上一行:
"miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
這里的miDebuggerPath即為gdb的安裝路徑。
切換到test.c頁面,按F5即可開始調試
也可使用windows的編譯工具,參見MSDN。不過我的output選項卡窗口中文都出現了亂碼