用vscode學習c語言。php
記錄vscode配置c語言編譯環境。c++
1.安裝vscode(版本1.27)shell
https://code.visualstudio.com/ 下載安裝vscode.json
2.安裝c/c++擴展。windows
3.安裝編譯工具mingw-w64,http://www.mingw-w64.org/doku.php/download工具
配置環境變量,以WIN10爲例 ,此電腦-屬性-高級系統設置-環境變量-系統變量-path-添加一條D:\Program Files\mingw-w64\i686-8.1.0-posix-dwarf-rt_v6-rev0\mingw32\bin(你安裝編譯工具路徑)學習
配置前ui
配置後編碼
-ps:若是開着vscode配置環境變量,配置完要關掉vscode重開一次。spa
4.配置文件launch.json,task.json。
新建文件hello.cpp,
按F5彈出選擇環境,配置launch.json
點擊進去,configurations:裏內容以下;
{ "name": "(gdb) Launch", "type": "cppdbg", "request": "launch", "program": "enter program name, for example ${workspaceFolder}/a.exe", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": true, "MIMode": "gdb", "miDebuggerPath": "/path/to/gdb", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ] }
修改爲以下;
{ "name": "(gdb) Launch", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/a.exe",//這裏刪除前面那裏的enter program name, for example "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": true, "MIMode": "gdb", "miDebuggerPath": "D:\\Program Files\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\bin\\gdb.exe",//修改成你安裝mingw32的路徑 "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "build hello",//task.json裏面的名字 }
回到hello.cpp按F5彈出報錯框,選配置任務。
點擊,而後選Others,出現task.json,以下;
"version": "2.0.0", "tasks": [ { "label": "echo", "type": "shell", "command": "echo Hello" } ]
修改以下;
"version": "2.0.0", "tasks": [ { "label": "build hello", "type": "shell", "command": "g++", "args": [ "-g", "hello.cpp", ], "group":{ "kind": "build", "isDefault": true } } ]
注意:若是是win32程序(窗口界面),args內加上"-mwindows"。
回到hello.cpp文件,按F5。成功運行編譯。
5.還有就是發現中文控制檯顯示亂碼,
只要點擊右下角utf-8,
點使用編碼保存,選GBK,而後F5運行
----------------------------------------------------------------------------------------------------------------------------------------
參考閱讀vscode文檔:https://code.visualstudio.com/docs/languages/cpp