vscode如何配置c/c++環境
下載
- Mingw
參考鏈接:https://blog.csdn.net/jiqiren_dasheng/article/details/103775488
筆者下載的x86_64-8.1.0-release-win32-sjlj離線包存放在百度網盤,需要的讀者可以前往取用。
鏈接:https://pan.baidu.com/s/17hp8J_VDJwc9HjuocPBn4A
提取碼:ulaj
安裝
安裝VS Code
下載好VS Code后,安裝時可以自己選擇安裝路徑,其他的添加在windows右鍵菜單創建
。
參見鏈接:https://www.php.cn/tool/vscode/450800.html
安裝Mingw-w64
安裝Mingw-w64時,在Architecture一欄如果32位就選i686,如果64位就選擇x86_64,其他的默認就好。安裝目錄我自定在d盤
安裝C/C++支持插件
打開VS Code在插件商店搜索C/C++這個插件進行安裝。
配置
新建一個文件夾,然后,右鍵選擇用VS Code打開,打開之后新建一個.c文件,進行環境配置。
配置 launch.json文件
點擊左邊活動欄的調試按鈕,然后,點擊配置按鈕選擇環境C++(GDB/LLDB);
之后在這個文件夾里會多出一個文件夾.vscode,這個文件夾里會生成一個json文件——launch.json,然后將下面的代碼替換掉里面的代碼;
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb)c_launch",
"type": "cppdbg",
"request": "launch",
"targetArchitecture": "x64",
"program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
"miDebuggerPath": "D:/init_all/vscode_editor/Mingwgcc/mingw64/bin/gdb.exe",
"MIMode": "gdb",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"environment": [],
"internalConsoleOptions": "openOnFirstSessionStart",
"externalConsole": true,
"preLaunchTask": "gcc"
}
]
}
其中,第12行的*targetArchitecture*
根據自己所需的構架來更改,第14行的*miDebuggerPath*
需要按照Mingw-w64的安裝目錄來更改,其他的默認就好;
配置 tasks.json文件
在狀態欄上選擇Terminal
,在下拉選項中選擇configTure Tasks...
;然后選擇使用模板創建 tasks.json 文件這個選項;
接着就是選擇Others 運行任意外部命令的示例這個選項;參考鏈接
隨之則會生成一個tasks.json的文件,然后將下面的代碼替換掉里面的代碼;
tasks.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"command": "gcc",
"args": ["-Wall", "-g", "${file}", "-o", "${fileBasenameNoExtension}.exe"],
"echoCommand": true,
"problemMatcher": {
"owner": "cpp",
"fileLocation": ["relative", "${workspaceFolder}"],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
},
"group": {
"kind": "build",
"isDefault": true
}
}
調試測試
#include <stdio.h>
int main(void)
{
int a = 1;
int b = 2;
int c = 0;
c = a + b;
printf("%d + %d = %d", a, b, c);
return 0;
}
還存在着一個問題——頭文件哪里會出現一條綠色的波浪線,並且沒有自動補全的功能;
配置 c_cpp_properties.json文件
光標放在頭文件哪里會出現一個黃色的小燈泡,點擊之后選擇Edit "includePath" setting,隨之會多出一個json文件c_cpp_properties.json,然后找到與Windows相關的頭文件路徑配置代碼,即在"name": "Win32"的下面。根據Mingw-w64的安裝路徑找到頭文件的所在路徑,分別放在includePath和path中,即是下面有注釋的地方:
c_cpp_properties.json
{
"configurations": [
{
"name": "Mac",
"includePath": [
"/usr/include",
"/usr/local/include",
"${workspaceRoot}"
],
"defines": [],
"intelliSenseMode": "clang-x64",
"browse": {
"path": [
"/usr/include",
"/usr/local/include",
"${workspaceRoot}"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
},
"macFrameworkPath": [
"/System/Library/Frameworks",
"/Library/Frameworks"
]
},
{
"name": "Linux",
"includePath": [
"/usr/include",
"/usr/local/include",
"${workspaceRoot}"
],
"defines": [],
"intelliSenseMode": "clang-x64",
"browse": {
"path": [
"/usr/include",
"/usr/local/include",
"${workspaceRoot}"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
}
},
{
"name": "Win32",
"includePath": [
"C:/mingw-w64/mingw64/include",
"C:/mingw-w64/mingw64/x86_64-w64-mingw32/include",
//根據Mingw-w64的安裝路徑更改
"${workspaceRoot}"
],
"defines": [
"_DEBUG",
"UNICODE"
],
"intelliSenseMode": "msvc-x64",
"browse": {
"path": [
"C:/mingw-w64/mingw64/include",
"C:/mingw-w64/mingw64/x86_64-w64-mingw32/include",
//根據Mingw-w64的安裝路徑更改
"${workspaceRoot}"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
}
}
],
"version": 3
}
現在沒有了綠色波浪線,也可以實現自動補全了,所以配置完成。
使用的時候注意用打斷點運行,先打斷點,然后F5;這樣才能看到黑窗口
以上內容參考鏈接有
主要配置和使用:https://www.jianshu.com/p/b7cc0e36cd5f
vscode右鍵項目文件夾打開方法:https://www.php.cn/tool/vscode/450800.html
MinGW-w64 C/C++編譯器下載和安裝
https://blog.csdn.net/jiqiren_dasheng/article/details/103775488
https://pan.baidu.com/s/17hp8J_VDJwc9HjuocPBn4A
寫本文的目的是結合自身實踐,有些地方有出入,我來分享我自己遇到的問題以及如何解決的。