众所周知,VScode是一个轻量,简便,功能强大的编辑器,我们可以在里面编写各种各样的代码,但是在C/C++代码编译运行的时候,我们需要对环境进行一些适配,废话不多说,请看下面的详细步骤。
一.下载VScode编辑器
我们找到VScode的官网,找到适合自己电脑的版本(Windows、Mac、Linux)等,下面以Windows来进行示例:下载地址:Download Visual Studio Code - Mac, Linux, Windows
二.下载MinGW-w64 - for 32 and 64 bit Windows
MinGW-w64 是一个用于在 Windows 操作系统上开发和构建原本为类 Unix 系统设计的软件的工具集。它提供了一个 POSIX(Portable Operating System Interface,可移植操作系统接口)环境,使得 Windows 用户可以使用许多在 Unix 系统上流行的工具和命令,从而增强了 Windows 系统的开发能力。MinGW-w64 的主要目标是提供一个最小化的、能够运行 GNU 工具链的 Windows 环境。它包括了许多常见的 GNU 工具和库,例如 GCC(GNU Compiler Collection,GNU 编译器集合)、GDB(GNU Debugger,GNU 调试器)等,以及一个 Windows 兼容的 POSIX API 实现。这些工具允许开发者在 Windows 上编写、编译和调试 C/C++ 等编程语言的代码,同时也能运行许多在 Unix 系统上编写的脚本和工具。
下载地址:MinGW-w64 - for 32 and 64 bit Windows download | SourceForge.net
我们下载完成后得到一个.7z的压缩包,我们拷贝一下放在一个全英文的路径下解压(重点),否则后续配置无法成功。
三.配置环境变量
我们先点开mingw64这个文件的bin目录,复制一下,拷贝在用户变量的Path路径中。此时,我们在桌面打开一个终端,输入g++ --version命令观察环境变量是否配置成功。
如果提示与上图一致,代表环境变量配置成功,若报错,请重新配置。
四.配置VScode基础项
1.汉化插件
第一次进入VScode,我们会发现怎么是全英文编辑器(救命,孩子英语不好),那么这个时候,我们就需要一个汉化插件来帮助我们提高开发效率了,我们先单击左侧的四个矩形标识的图标,这便是管理拓展项的开关,我们搜索Chinese,第一个便就是我们的汉化插件,把它install下来重启VScode即可。2.C/C++插件
我们要编译运行C/C++文件的话还需要一个C/C++插件,我们在扩展里面直接搜索C/C++即可,同理install下来重启VScode就能使用了。五.配置.vscode文件环境基础配置项
我们在全英文目录下新建一个文件夹Code,我们使用VScode打开,并在里面配置.vscode文件,若初始化没有生成,则需自己创建。1.c_cpp_properties.json
**用途**: 配置 C/C++ 语言相关的属性和环境设置{"configurations": [{"name": "Win64","includePath": ["${workspaceFolder}/**"],"defines": ["_DEBUG", "UNICODE", "_UNICODE"],"windowsSdkVersion": "10.0.18362.0","compilerPath": "D:/C++Surroundings/mingw64/bin/g++.exe","cStandard": "c17","cppStandard": "c++17","intelliSenseMode": "gcc-x64"}],"version": 4
}
其中:compilerPath路径需找到自己本地的相应路径进行配置
2.launch.json
**用途**:用于配置调试相关的设置。如前面介绍的用于 C/C++ 调试时,指定调试器类型、可执行文件路径、调试模式等。{"version": "0.2.0","configurations": [{"name": "(gdb) Launch","type": "cppdbg","request": "launch","program": "${fileDirname}\\${fileBasenameNoExtension}.exe","args": [],"stopAtEntry": false,"cwd": "${workspaceRoot}","environment": [],"externalConsole": true,"MIMode": "gdb","miDebuggerPath": "D:\\C++Surroundings\\mingw64\\bin\\gdb.exe","preLaunchTask": "g++","setupCommands": [{"description": "Enable pretty-printing for gdb","text": "-enable-pretty-printing","ignoreFailures": true}]}]
}
其中:miDebuggerPath路径需找到自己本地的相应路径进行配置
3.settings.json
**用途** :用于配置编辑器本身的各种通用设置{"files.associations": {"*.py": "python","iostream": "cpp","*.tcc": "cpp","string": "cpp","unordered_map": "cpp","vector": "cpp","ostream": "cpp","new": "cpp","typeinfo": "cpp","deque": "cpp","initializer_list": "cpp","iosfwd": "cpp","fstream": "cpp","sstream": "cpp","map": "c","stdio.h": "c","algorithm": "cpp","atomic": "cpp","bit": "cpp","cctype": "cpp","clocale": "cpp","cmath": "cpp","compare": "cpp","concepts": "cpp","cstddef": "cpp","cstdint": "cpp","cstdio": "cpp","cstdlib": "cpp","cstring": "cpp","ctime": "cpp","cwchar": "cpp","exception": "cpp","ios": "cpp","istream": "cpp","iterator": "cpp","limits": "cpp","memory": "cpp","random": "cpp","set": "cpp","stack": "cpp","stdexcept": "cpp","streambuf": "cpp","system_error": "cpp","tuple": "cpp","type_traits": "cpp","utility": "cpp","xfacet": "cpp","xiosbase": "cpp","xlocale": "cpp","xlocinfo": "cpp","xlocnum": "cpp","xmemory": "cpp","xstddef": "cpp","xstring": "cpp","xtr1common": "cpp","xtree": "cpp","xutility": "cpp","stdlib.h": "c","string.h": "c"},"editor.suggest.snippetsPreventQuickSuggestions": false,"aiXcoder.showTrayIcon": true
}
4.tasks.json
**用途**:用于定义各种任务,比如编译任务等。对于 C/C++ 开发,通常用于配置如何使用编译器(如 MinGW-w64 中的 GCC)编译代码。{"version": "2.0.0","tasks": [{"label": "g++","command": "g++","args": ["-g","${file}","-o","${fileDirname}\\${fileBasenameNoExtension}.exe"],"problemMatcher": {"owner": "cpp","fileLocation": ["relative", "${workspaceRoot}"],"pattern": {"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$","file": 1,"line": 2,"column": 3,"severity": 4,"message": 5}},"group": "build"},{"type": "cppbuild","label": "C/C++: g++.exe 生成活动文件","command": "D:/C++Surroundings/mingw64/bin/g++.exe","args": ["-fdiagnostics-color=always","-g","${file}","-o","${fileDirname}\\${fileBasenameNoExtension}.exe"],"options": {"cwd": "D:/C++Surroundings/mingw64/bin"},"problemMatcher": ["$gcc"],"group": {"kind": "build","isDefault": true},"detail": "调试器生成的任务。"}]
}
其中:command,cwd路径需找到自己本地的相应路径进行配置
六.创建.c和.cpp文件调试运行
我们在与.vscode同级的目录下创建一个全英文的新文件,放置我们的.cpp和.c文件,我们试着来编写一段程序,如下图所示:如果你发现自动生成了一个.exe文件代表着编译成功,当我们看到终端正确打印出来相应的内容就证明我们完成了本次的配置,以后就可以愉快地在VScode中编写C/C++代码了,到这里,恭喜你学会了配置环境,快去帮你的同学或室友配置一下吧!