g++的安裝過程忽略,記不清有沒有“安裝路徑不能有空格”這種問題。
網上翻了幾個博客,找到的配置文件在g++下都不能運行,遂折騰。
安裝vscode與插件
插件為ms-vscode.cpptools(不知道是不是默認安裝的,如果不是點左方extension圖標,可以搜索c++下載,也可以下載其他代碼補全插件)
設置tasks.json文件
此文件主要指定編譯器相關設置,使編譯程序能夠將源代碼編譯為exe
- 首先用vs code打開一個文件夾
- 然后ctrl+shift+p打開vs code的命令行
- 輸入並選擇tasks: configure task runner
- 然后會出現幾種語言類型模板
- 選擇other后會創建一個新的tasks.json文件
(也可以在指定文件夾下手動創建空文件)
向文件中粘貼以下內容:
{
"version" : "2.0.0" ,
"isShellCommand" : true ,
"tasks" : [
{
"taskName" : "build" ,
"type" : "shell" ,
"command" : "g++" ,
"args" : [
"-g" , "tm.cpp"
]
}
]
}
此時ctrl+shift+b可以編譯文件
設置launch.json文件
點f5調試會提示創建此文件,也可手動創建。
粘貼以下代碼,F5即可進行調試
{
"version": "2.0.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
// name of exe program
"program": "${workspaceRoot}/a.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
// g++ path
"miDebuggerPath": "D:\\Program_Files_portable\\MinGW-0.6.2\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build"
}
]
}