VScode C/C++ 环境及 C/C++ Compile Run 配置
rt, 咕。
喜报:cao2333在机房与VScode大战 114514min 后成功调好VScode !
下载
请去 官网直接下载
MinGW
- 直接搜
MinGW,不要直接下载,在下滑之后找到适合自己的版本再下载可以看看别人的文章咕。 - 如果你有Dev c++:
- 找到路径
~\Dev-Cpp\MinGW64\bin,里面有你接下来需要的所有配置文件
- 找到路径
配置方式其他文章已经很详细了,一搜一大片而且没有冲突。照着直接莽咕
无网络情况下:
先用 VScode 打开目标文件 xxx.cpp 所在的文件夹(确保路径正确),然后按 Ctrl+` 即可(`键是键盘左上角或~同键)
呼出命令提示符以后,输入:
g++ xxx.cpp -o xxx.exe -std=c++14 -O2 -lm;
上面是编译。 编译之前一定要先保存代码!确保你的路径无误!
然后输入
.\xxx.exe
这里的.exe文件不需要与.cpp同名。上述两行可以合为一行咕。
如果你是 NOI Linux 环境, 直接把上述过程的 .exe 后缀直接删掉即可。
有网环境(推荐)
插件下载:
点击左侧下面四个方块(右上角转了一下那个咕)
- 搜索
Chinese下载中文简体蓝勾汉化包(可选) - 搜索
C/C++下载C/C++ Extension Pack整合包,有四个扩展,等待下载完成即可。(必选) - 推荐下载
C/C++ Compile Run,下文配置默认你已有该插件。 - 推荐主题:
Woodfish Theme彩虹主题,比较炫酷,可以在设置上自行调整效果样式 - 如果你要写题解的话,别忘了
Markdown All in One和LaTeX咕。
内置设置
经众人检验,一般只有无网模式终端编译能够绕过这一步。无论你用哪一种方式,都必须按照下述配置。
- 在你打代码的地方新建
.vscode文件夹。 - 在内部新建两个文件:
launch.jsontask.json
在 launch.json 中粘贴:
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) 启动",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\run.exe",
//建议改成这样,以后都会在同一个run.exe文件中运行,比较省硬盘空间
"args": [//这几行是你在调试的时候加入的命令
"-std=c++14",
"-O2",
"-lm"
],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false, //修改此项会让其弹出终端,鼠标悬停有说明
"MIMode": "gdb",
"miDebuggerPath": "C:\\Program Files (x86)\\Dev-Cpp\\MinGW64\\bin\\gdb.exe",
//上面这行是你的gdb路径
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"preLaunchTask": "编译:"//修改此项,要求与下文tasks.json统一。
},
{
"name": "C/C++: g++.exe 构建和调试活动文件",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\run.exe",
"args": [
"-std=c++14",
"-O2",
"-lm"
],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\Program Files (x86)\\Dev-Cpp\\MinGW64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe 生成活动文件"
}
]
}
在tasks.json 中粘贴:
{
"version": "2.0.0",
"tasks": [
{
"label": "编译:",//要求与上文相同,其他随便改
"type": "process", //选择 shell 或 process, 鼠标悬停有说明,下文不再赘述
"command": "C:\\Program Files (x86)\\Dev-Cpp\\MinGW64\\bin\\g++.exe",
//上面是你的g++路径
"args": [ //编译时加入的命令
"-fdiagnostics-color=always",
"-g++",
// "${file}",
"*.cpp", //这个和下面的推荐改掉,应该和默认的(注释版)没什么区别
"-o",
// "${fileDirname}\\${fileBasenameNoExtension}.exe",
"${fileDirname}\\run.exe",//和上面*.cpp一组
"-Wall",
"-static-libgcc",
"-Wno-format",
"-std=c++14",
"-O2",
"-lm",
"-fexec-charset=ASKII"//防止乱码
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": false
},
"detail": "编译器: C:\\Program Files (x86)\\Dev-Cpp\\MinGW64\\bin\\g++.exe"
//似乎有没有都可以,这只是给我们看的,对计算机而言没啥用
},
{//一定要加这一份任务组,可以在 “终端>配置默认生成任务>C/C++开头的所有(其实就2个)” 点击修改即可
"type": "cppbuild",
"label": "C/C++: g++.exe 生成活动文件",
"command": "C:\\Program Files (x86)\\Dev-Cpp\\MinGW64\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
// "${file}",
"*.cpp",
"-o",
// "${fileDirname}\\${fileBasenameNoExtension}.exe",
"${fileDirname}\\run.exe",
"-std=c++14",
"-O2",
"-lm"//同上
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
},
{//下面这些是我与Compile大战的结果,一直提示我没有这个任务干脆自己造了一份
"type": "process",//同上。
"command":"C:\\Program Files (x86)\\Dev-Cpp\\MinGW64\\bin\\g++.exe",
"problemMatcher": [
"$gcc"
],
"label": "C/C++ Compile Run: C/C++ Compile Run: Compile",//一定要一字不差!
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
上述两个文件都直接存在你新建的 .vscode 文件下。
设置
在最顶上的细长框里输入并找到:
> Open User Settings (JSON)
点击并打开,输入(或修改)下面这些:
警告:请只是添加或者修改原有参数,不要直接覆盖,否则有可能损坏原设置!
//这些都是有鼠标悬停提示的,可以自己改
{
"editor.fontSize": 14, // 字体大小
"files.autoSaveDelay": 1000, // 自动保存延迟时间(毫秒)
"files.autoSave": "afterDelay",//自动保存
"editor.tabSize": 4, // 设置制表符大小
"explorer.confirmDelete": false,
"workbench.colorTheme": "Woodfish Dark",//主题
"editor.mouseWheelZoom": true,
"woodfishTheme.glow.enabled": false,//一些自己就可以改的地方
"editor.cursorSmoothCaretAnimation": "on",
"workbench.list.smoothScrolling": true,
"terminal.integrated.smoothScrolling": true,
"editor.smoothScrolling": true,
"editor.cursorBlinking": "phase",
"git.ignoreLegacyWarning": false,
"editor.autoClosingDelete": "always",
//下面直接看前面的键值就知道是干嘛的了,不再赘述
"c-cpp-compile-run.cpp-linker-flags": "-std=c++14 -O2 -lm",
"c-cpp-compile-run.run-args": "-std=c++14 -O2 -lm",
"c-cpp-compile-run.debugger-path": "C:/Program Files (x86)/Dev-Cpp/MinGW64/bin/gdb.exe",
"c-cpp-compile-run.cpp-compiler": "C:\\Program Files (x86)\\Dev-Cpp\\MinGW64\\bin\\g++.exe",
"c-cpp-compile-run.c-compiler": "C:\\Program Files (x86)\\Dev-Cpp\\MinGW64\\bin\\gcc.exe",
"c-cpp-compile-run.run-in-external-terminal": true,
"C_Cpp.default.intelliSenseMode": "windows-gcc-x64",//下面是一些C++底层设置,最好别动
"C_Cpp.errorSquiggles": "enabled",
"C_Cpp.autocompleteAddParentheses": true,
"C_Cpp.default.cppStandard": "c++14",
"C_Cpp.default.cStandard": "c11",
"C_Cpp.workspaceParsingPriority": "low",//这个能显著减轻VScode给CPU的压力
"workbench.colorTheme": "Woodfish Dark",
"editor.trimAutoWhitespace": false,//把这个改成false VScode就不会删你的行末空格了
//下面这些因插件而异吧,加不加都行,似乎没啥用
"[cpp]": {
"editor.codeLens": true,
"editor.wordBasedSuggestions": "off",
"editor.semanticHighlighting.enabled": true,
"editor.stickyScroll.defaultModel": "foldingProviderModel"
},
"workbench.editorAssociations": {
"*.copilotmd": "vscode.markdown.preview.editor",
"*.exe": "default"
},
"editor.unicodeHighlight.invisibleCharacters": false,
"markdown.extension.completion.enabled": true,
"html.suggest.hideEndTagSuggestions": true,
}
//该配置文件存储在 C:\Users\Admin\AppData\Roaming\Code\User\setting.json 下。
代码片段
在 设置>代码片段>c++ 中,插入下面这些:
//我的代码片段
{
"p1": {//这个名随便起
"prefix": "BEGIN",//效果:在任意文件处输入BEGIN,选中BEGIN提示按Enter会自动打出下面这些
"body": [
"#include <bits/stdc++.h>",
"using namespace std;",
"const int N=1e6+7;",
"",
"inline void read(int &x){",
" x=0;int f=1; char ch=getchar();",
" while(!isdigit(ch)){",
" if(ch=='-') f=-1;",
" ch=getchar();",
" }",
" while(isdigit(ch)){",
" x=(x<<3)+(x<<1)+(ch^48);",
" ch=getchar();",
" }",
" x*=f;",
"}",
"",
"inline void write(int x){",
" if(x<0) putchar('-'),x*=-1;",
" if(x>9) write(x/10);",
" putchar(x%10+'0');",
"}",
"",
"int n;",
"int main() {",
"#ifndef ONLINE_JUDJE",
" freopen(\"ac.in\",\"r\",stdin);",
" freopen(\"ac.out\",\"w\",stdout);",
"#endif",
" read(n);",
" $0",
" return 0;",
"}"
],
"description": "Open with Code"
},
"p2":{
"prefix": "FASTIO",//同上
"body": [
"inline void read(int &x){",
" x=0;int f=1; char ch=getchar();",
" while(!isdigit(ch)){",
" if(ch=='-') f=-1;",
" ch=getchar();",
" }",
" while(isdigit(ch)){",
" x=(x<<3)+(x<<1)+(ch^48)",
" ch=getchar();",
" }",
" x*=f;",
"}",
"",
"inline void write(int x){",
" if(x<0) putchar('-'),x*=-1;",
" if(x>9) write(x/10);",
" putchar(x%10+'0');",
"}",
""
],
"description": "Faster in and out"
}
}
//该文件将存储在 C:\Users\Admin\AppData\Roaming\Code\User\snippets\cpp.json下。
杂项
之后,还有几个建议的小设置:
在左下角齿轮标处打开设置面板:
- 搜索
Smoth,下面看描述和鼠标悬停提示自己改个好看的咕 - 搜索
Ctrl, 同样看描述改(看不懂的不要瞎改)- 把
Editor: Mouse Wheel Zoom打开,就可以像Dev和浏览器里一样按Ctrl+滚轮改界面大小了咕~
- 把
剩下的还没想起来,dalao可以分享感谢!
调好之后,按右上角的三角或者 F5 F6 就能编译运行了咕。
以上。感谢阅读,记得给一个推荐让更多人看到咕!
\[The End
\]

浙公网安备 33010602011771号