vscode配置c++环境
-
下载MinGW-w64,下载链接:
https://winlibs.com/
,下载最新版 -
设置windows环境变量,需要提到最前面,
D:\mingw64\bin
(以前可能有老版本会干扰) -
测试环境,gcc --version,g++ --version
-
创建测试代码,然后编译,gcc hello.c -o hello.exe
-
vscode安装拓展
- Code Runner:提供一个按钮,运行一个特定的指令,可以一键编译运行
- C/C++:提供 C/C++ 代码高亮、静态分析等功能
- vscode-icons:更好的文件图标显示(安装后记得 enable)
- One Dark Pro 和 GitHub Theme:个人比较喜欢的颜色主题
- GlassIt-VSC:按
Ctrl
+Alt
+z
变透明,按Ctrl
+Alt
+c
变回来 - Office Viewer(Markdown Editor):我用它打开 pdf 和图片
- 按
Ctrl
+,
,或者左下角的齿轮 -Settings
-
全局配置设置
- Code-runner: Save File Before Run 设为 true
- Code-runner: Run In Terminal 设为 true
- Code-runner: Ignore Selection 设为 true
- Editor: Format On Save 设为 true
- Editor: Format On Type 设为 true
- Editor: Accept Suggestion On Enter 个人建议设为 off,避免和换行冲突(按
Tab
仍然可以接受补全)
-
工作区配置
- settings.json(这个可以直接在全局文件中写的):
Windows: { "code-runner.executorMap": { "cpp": "cd $dir && g++ '$fileName' -o '$fileNameWithoutExt.exe' -std=c++20 -Wall -Wpedantic -Wextra && echo 'compilation ends.' && .\\'$fileNameWithoutExt'", "c": "cd $dir && gcc '$fileName' -o '$fileNameWithoutExt.exe' -std=c17 -Wall -Wpedantic -Wextra && echo 'compilation ends.' && .\\'$fileNameWithoutExt'" } }
- c_cpp_properties.json(这个去工作区中自己手动设置好就可以了):
{ "configurations": [ { "name": "Win32", "compilerPath": "C:\\mingw64\\bin\\gcc.exe", "cStandard": "c17", "cppStandard": "c++20", "intelliSenseMode": "windows-gcc-x64", "args": [], "compilerArgs": [ "-Wall", "-Wpedantic", "-Wextra" ] } ], "version": 4 }
-
Debugger打开 hello.c,点击 运行 - 开始调试(快捷键 F5),选 C++ (GDB/LLDB),选 gcc。