Win10下配置VScode的C/C++開發環境
VScode環境搭建
1. MinGW安裝配置
MinGW下載安裝
MinGW是什麼這裡就不進行介紹了,下面直接進入安裝的正題。MinGW的下載連結:https://osdn.net/projects/mingw/releases/, 在網站中可以下載到mingw-get-setup.exe
安裝的時候根據自己的需求進行設定,不知道如何設定的可以直接使用預設值,安裝完成後會自動開啟MinGW Installing Manager,如下:
這裡我們主要配置C/C++開發環境,所以選擇
minw32-gcc-g++
即可,當然你可以全部選中。然後點選選單欄"Installation"-"Apply changes"應用更改。具體操作可參照MinGW安裝教程[TZZ]
設定環境變數
安裝完成MinGW後將其新增至環境變數,即把C:\MinGW\bin
新增至系統環境變數。
注意:這裡的C:\MinGW\bin
與你的安裝路徑有關!
下面在命令提示符或者Windows PowweShell中輸入g++ -v
2. Vscode安裝配置
Vscode安裝
在Vscode官網下載安裝包:https://code.visualstudio.com/
安裝完成後開啟Vscode,主介面如下:
安裝C/C++外掛
點選左側Extension按鈕來選擇安裝外掛:
搜尋C++,點選Install
新建C/C++工程
由於Vscode以資料夾的形式管理工程,所以需要新建一個資料夾來管理工程,這裡建立一個名為hello的資料夾。
然後用Vscode開啟該資料夾:
開啟後得到一個空的工程:
快捷鍵Ctrl+N新建一個C++檔案,取名為helloworld.cpp,並輸入一個簡單的程式:
啟動配置檔案(launch.json)
點選左邊欄的Debug按鈕,然後選擇配置launch.json
檔案,操作如下:
選擇C++(GDB\LLDB)
,自動開啟launch.json檔案:
launch.json
初始內容如下:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"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
}
]
}
]
}
修改為如下內容並儲存:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "C:/MinGW/bin/gdb.exe",
"preLaunchTask": "build",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
注意
"miDebuggerPath": "C:/MinGW/bin/gdb.exe"
與你安裝的路徑有關!
"preLaunchTask": "build"
表示設定啟動前的任務為"build",下面通過tasks.json來新增"build"任務。
新增編譯任務(tasks.json)
利用快捷鍵ctrl+shift+p開啟命令列,輸入Tasks: Run task
,然後依次選擇:
> No task to run found. configure tasks...
> Create tasks.json file from template
> Others
Example to run an arbitrary external command.
自動開啟的tasks.json檔案初始內容如下:
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "echo",
"type": "shell",
"command": "echo Hello"
}
]
}
將其修改為如下內容並儲存:
// Available variables which can be used inside of strings.
// ${workspaceRoot}: the root folder of the team
// ${file}: the current opened file
// ${fileBasename}: the current opened file's basename
// ${fileDirname}: the current opened file's dirname
// ${fileExtname}: the current opened file's extension
// ${cwd}: the current working directory of the spawned process
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared"
},
"windows": {
"command": "g++",
"args": [
"-ggdb",
"\"${file}\"",
"--std=c++11",
"-o",
"\"${fileDirname}\\${fileBasenameNoExtension}.exe\""
]
}
}
]
}
至此,我們已經完成的C/C++的基本開發環境,可以利用Vscode進行程式碼編輯、編譯、除錯等功能。此外,Vscode支援Linux系統,關於在Linux系統下配置基於Vscode的C/C++開發環境可參考另一篇博文:https://blog.csdn.net/weixin_43374723/article/details/84064644