十六、windows11下VSCode配置C/C++编译环境
- 1.安装VSCode
- 2.中文插件
- 3.MinGW编译器下载和配置
- 4.VSCode配置c/c++编译环境
- 5.测试是否配置成功
- 6.使用万能头文件 #include <bits/stdc++.h>
1.安装VSCode
下载安装
![在这里插入图片描述](https://img-blog.csdnimg.cn/103f190985694d45b6a3079f44193c6f.png)
![在这里插入图片描述](https://img-blog.csdnimg.cn/f91558c2c54549018abf233904dd5b3c.png)
![在这里插入图片描述](https://img-blog.csdnimg.cn/ecb2fe883510427fb283133b7372fd49.png)
![在这里插入图片描述](https://img-blog.csdnimg.cn/49653c1d5fe1483388eda9d67c8c59d0.png)
![在这里插入图片描述](https://img-blog.csdnimg.cn/74c132438783499da5c3d6b0ddc61047.png)
2.中文插件
![在这里插入图片描述](https://img-blog.csdnimg.cn/79713e4248ea4d1598dea791c5e7ccc4.png)
3.MinGW编译器下载和配置
Windows和Ubuntu不同,Ubuntu是自带gcc的,但是Windwos并没有自带C/C++的编译器,需要自己下载
MinGW 的全称是:Minimalist GNU on Windows
它实际上是将经典的开源 C语言 编译器 GCC 移植到了 Windows 平台下,并且包含了 Win32API ,因此可以将源代码编译为可在 Windows 中运行的可执行程序。而且还可以使用一些 Windows 不具备的,Linux平台下的开发工具。一句话来概括:MinGW 就是 GCC 的 Windows 版本
。
MinGW-w64 与 MinGW 的区别在于 MinGW 只能编译生成32位可执行程序,而 MinGW-w64 则可以编译生成 64位 或 32位 可执行程序。正因为如此,MinGW 现已被 MinGW-w64
所取代。
下载 MinGW-w64 编译器
![在这里插入图片描述](https://img-blog.csdnimg.cn/0c538e5ef2f745c6be36f2de6e6735f3.png)
下载好这个压缩包之后,解压
![在这里插入图片描述](https://img-blog.csdnimg.cn/c9f7c67e866340d6ba5916ad0e059a7e.png)
![在这里插入图片描述](https://img-blog.csdnimg.cn/33263fa933ee4d0ca3a3702a9baf1d14.png)
然后将里面的文件,放到一个不含中文路径的目录,比如我这里将这些文件复制到D:\01Software\MinGW
![在这里插入图片描述](https://img-blog.csdnimg.cn/191f0c62ea5a41859669e102c4fd225a.png)
然后复制文件里bin目录的路径(后面配置环境变量时用得到)
![在这里插入图片描述](https://img-blog.csdnimg.cn/e771cbd1d3f347eab2658b3e9a9f718d.png)
bin目录下的这两个可执行文件,分别是C++的编译器、C的编译器,我们就是需要这两个东西
后面安装VSCode的C/C++插件,也是去调用这两个可执行文件
![在这里插入图片描述](https://img-blog.csdnimg.cn/8b2f5979ddea49b68ad84a7a5393a338.png)
然后这个 gdb.exe 就是调试器
![在这里插入图片描述](https://img-blog.csdnimg.cn/c899a9887e7c49c8b515de5a954b03b1.png)
在系统环境变量配置path变量:Win + Q,在搜索栏中输入环境变量
![在这里插入图片描述](https://img-blog.csdnimg.cn/423264b056af4989aa44f1f78b5b79f7.png)
![在这里插入图片描述](https://img-blog.csdnimg.cn/ec69cc60991240008c7e86e383d9f43b.png)
找到Path——编辑
![在这里插入图片描述](https://img-blog.csdnimg.cn/c2dbe6f94403418caa071e1bdeaa3115.png)
新建
![在这里插入图片描述](https://img-blog.csdnimg.cn/d7e30ba243f4409aaf306dedda9310f7.png)
把下载的MinGW里的bin目录路径粘贴在这里,我的电脑是D:\01Software\MinGW\bin
![在这里插入图片描述](https://img-blog.csdnimg.cn/db30341768154eaf8fb46af72a301339.png)
检查是否配置成功
-
打开cmd,
-
进入bin目录
-
输入gcc -v或gcc -v或g++ -v
![在这里插入图片描述](https://img-blog.csdnimg.cn/dbad0332ba8648b7b4e19cf07b1a7d07.png)
OK,MinGW-w64 配置成功
实际上配置环境变量的目的就是:让你更好的打开软件,和操作软件,其实如果不配置,进入到软件的安装位置,同样可以打开的,但是配置了环境变量之后,当我们需要调用这些软件时,系统会自动帮我们去寻找环境变量中的资源
所以,其实现在我们在任何一个目录下,输入 gcc -v 都是 OK 的
![在这里插入图片描述](https://img-blog.csdnimg.cn/a231023a4db74e3aace4f5cd53fd5c60.png)
4.VSCode配置c/c++编译环境
下载C/C++插件
![在这里插入图片描述](https://img-blog.csdnimg.cn/b8d58ccd1dba40b388c07ffa9a5178d2.png)
安装之后重启一下VSCode
然后我们新建一个文件夹,用于放置我们的工程,文件名cpp demo
进入这个文件夹,右键在终端打开,输入code .
即可快速在VSCode中打开这个工程
![在这里插入图片描述](https://img-blog.csdnimg.cn/841ad0dac6d7451d90c0875768d4c9c5.png)
可以看到目前这个文件夹里面是空的,啥也没有
![在这里插入图片描述](https://img-blog.csdnimg.cn/5c03658889ad4627a494316c1eeda08c.png)
在vsCode文件夹下创建一个.vscode文件夹并创建3个文件
分别是c_cpp_properties.json,launch.json,tasks.json
![在这里插入图片描述](https://img-blog.csdnimg.cn/23a8eb7649ec4204b51773bece0645c2.png)
然后分别复制下面的内容到这三个文件中
c_cpp_properties.json
:
{
"configurations": [
{
"name": "Win32",
"includePath": ["${workspaceFolder}/**"],
"defines": ["_DEBUG", "UNICODE", "_UNICODE"],
"windowsSdkVersion": "10.0.17763.0",
"compilerPath": "D:\\01Software\\MinGW\\bin\\g++.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "${default}"
}
],
"version": 4
}
launch.json
:
{
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "D:\\01Software\\MinGW\\bin\\gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "task g++"
}
]
}
tasks.json
:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "task g++",
"command": "D:\\01Software\\MinGW\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-I",
"D:\\05cpp demo",
"-std=c++17"
],
"options": {
"cwd": "D:\\01Software\\MinGW\\bin"
},
"problemMatcher":[
"$gcc"
],
"group": "build",
}
]
}
5.测试是否配置成功
新建一个helloworld.cpp,按F5
运行
#include <stdio.h>
#include <windows.h>
int main()
{
printf("Hello World\n");
system("pause");
return 0;
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/eeffdda850af457eb33f991bce124d2d.png)
可以看到安装了插件之后,就可以调用g++.exe这个编译器了!!!很方便,不需要手动输入编译指令了
![在这里插入图片描述](https://img-blog.csdnimg.cn/14e35219d5fd41a5bb24722bfd038163.png)
6.使用万能头文件 #include <bits/stdc++.h>
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cuchar>
#include <cwchar>
#include <cwctype>
#endif
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <codecvt>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
MinGW中是自带这个万能头文件的,路径如下
D:\01Software\MinGW\lib\gcc\x86_64-w64-mingw32\8.1.0\include\c++\x86_64-w64-mingw32\bits
![在这里插入图片描述](https://img-blog.csdnimg.cn/0d65276585524be69601c1d7db3eee63.png)
但是在 helloworld.cpp 中直接#include <bits/stdc++.h>
是用不了的,编译会报错,这是为什么呢?
打开bits/stdc++.h
,我们会看到
这个头文件最后面:和网上的万能头文件相比,多了一块儿这个声明,导致报错,删除这一块儿进行了
![在这里插入图片描述](https://img-blog.csdnimg.cn/388923dbc45640659685ccd73b94195e.png)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)