vscode配置C/C++環境
1、下載安裝VScode
從官網下載(地址 https://code.visualstudio.com/)安裝包,安裝路徑按自己喜好安裝,
2、配置中文環境
以管理員身份運行vscode后,如果不喜歡英文界面,下面是中文環境,這是我已經安裝好的,

3、下載MinGW
MinGW在官網下載(https://sourceforge.net/projects/mingw-w64/)安裝包,點File按鈕,然后選最新版的安裝包


4、配置環境變數
把安裝好的MinGW的路徑拷貝下來,電腦桌面搜索欄搜索高級設定,然后點進去再點環境變數(N)

在最上方的admin的用戶變數(U)中找到path點它下方的編輯

再點新建,粘貼剛剛拷貝的路徑,然后點確定,再點外面彈窗的確定就完成了

回到桌面,快捷鍵按win+r會彈出一個視窗輸入cmd回車,在彈出的視窗輸入g++或者gcc回車,g++(或者gcc)如果提示的是 fatal error: no input files 則環境變數配置成功,如果輸入gcc -v則驗證成功,

5、安裝C/C++擴展

在路徑中新建檔案夾,然后打開新建好的檔案夾,在該檔案夾下新建檔案命名為hello.cpp檔案

6、配置C/C++環境
按快捷鍵ctrl+shift+p調出命令面板,輸入C/C++,然后選擇“Edit Configrations(UI)”進入配置

選末尾有g++.exe的

配置完成后會發現左邊建好的檔案夾下多了一個.vscode檔案夾,里面有c_cpp_properties.json檔案

c_cpp_properties.json檔案為:
{
“configurations”: [
{
“name”: “Win32”,
“includePath”: [
“${workspaceFolder}/**”
],
“defines”: [
“_DEBUG”,
“UNICODE”,
“_UNICODE”
],
“windowsSdkVersion”: “10.0.17763.0”, //這個不用理
“compilerPath”: “E:/codeconfig/mingw64/bin/g++.exe”,
“cStandard”: “c11”,
“cppStandard”: “c++17”,
“intelliSenseMode”: “gcc-x64” //這里改成gcc-x64就行了
}
],
“version”: 4
}
再按快捷鍵ctrl+shift+p,輸入tasks,選擇“Tasks:Configure Default Build Task”:
再選擇再選擇“C/C++: g++.exe build active file”:
此時出現一個tasks.json檔案,

tasks.json檔案代碼為:
{
“tasks”: [
{
“type”: “shell”,
“label”: “g++.exe build active file”, //剛才面板中選擇的
“command”: “E:\codeconfig\mingw64\bin\g++.exe”,
“args”: [
“-g”,
“
f
i
l
e
"
,
"
?
o
"
,
"
{file}", "-o", "
file","?o","{fileDirname}\
f
i
l
e
B
a
s
e
n
a
m
e
N
o
E
x
t
e
n
s
i
o
n
.
e
x
e
"
]
,
"
o
p
t
i
o
n
s
"
:
"
c
w
d
"
:
"
E
:
c
o
d
e
c
o
n
f
i
g
m
i
n
g
w
64
b
i
n
"
,
"
p
r
o
b
l
e
m
M
a
t
c
h
e
r
"
:
[
"
{fileBasenameNoExtension}.exe" ], "options": { "cwd": "E:\\codeconfig\\mingw64\\bin" }, "problemMatcher": [ "
fileBasenameNoExtension.exe"],"options":"cwd":"E:codeconfigmingw64bin","problemMatcher":["gcc”
],
“group”: “build”,
}
],
“version”: “2.0.0”
}
這時.vscode檔案夾中產生一個launch.json檔案,點F5進行除錯,然后選擇C++(GDB/LLDB):

launch.json檔案代碼為:
{
// 使用 IntelliSense 了解相關屬性,
// 懸停以查看現有屬性的描述,
// 欲了解更多資訊,請訪問: 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": "E:\\codeconfig\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb ",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "g++.exe build active file"
}
]
}
最后按F5除錯運行hello.cpp檔案

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/258840.html
標籤:其他
