十六、windows11下VSCode配置C/C++编译环境

2023-05-16

十六、windows11下VSCode配置C/C++编译环境

    • 1.安装VSCode
    • 2.中文插件
    • 3.MinGW编译器下载和配置
    • 4.VSCode配置c/c++编译环境
    • 5.测试是否配置成功
    • 6.使用万能头文件 #include <bits/stdc++.h>

1.安装VSCode

下载安装
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2.中文插件

在这里插入图片描述

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 编译器
在这里插入图片描述
下载好这个压缩包之后,解压
在这里插入图片描述
在这里插入图片描述
然后将里面的文件,放到一个不含中文路径的目录,比如我这里将这些文件复制到D:\01Software\MinGW
在这里插入图片描述
然后复制文件里bin目录的路径(后面配置环境变量时用得到)
在这里插入图片描述
bin目录下的这两个可执行文件,分别是C++的编译器、C的编译器,我们就是需要这两个东西
后面安装VSCode的C/C++插件,也是去调用这两个可执行文件
在这里插入图片描述

然后这个 gdb.exe 就是调试器
在这里插入图片描述

在系统环境变量配置path变量:Win + Q,在搜索栏中输入环境变量
在这里插入图片描述
在这里插入图片描述
找到Path——编辑
在这里插入图片描述

新建
在这里插入图片描述

把下载的MinGW里的bin目录路径粘贴在这里,我的电脑是D:\01Software\MinGW\bin
在这里插入图片描述
检查是否配置成功

  1. 打开cmd,

  2. 进入bin目录

  3. 输入gcc -v或gcc -v或g++ -v
    在这里插入图片描述

OK,MinGW-w64 配置成功
实际上配置环境变量的目的就是:让你更好的打开软件,和操作软件,其实如果不配置,进入到软件的安装位置,同样可以打开的,但是配置了环境变量之后,当我们需要调用这些软件时,系统会自动帮我们去寻找环境变量中的资源
所以,其实现在我们在任何一个目录下,输入 gcc -v 都是 OK 的
在这里插入图片描述

4.VSCode配置c/c++编译环境

下载C/C++插件
在这里插入图片描述
安装之后重启一下VSCode

然后我们新建一个文件夹,用于放置我们的工程,文件名cpp demo
进入这个文件夹,右键在终端打开,输入code .
即可快速在VSCode中打开这个工程
在这里插入图片描述

可以看到目前这个文件夹里面是空的,啥也没有
在这里插入图片描述

在vsCode文件夹下创建一个.vscode文件夹并创建3个文件
分别是c_cpp_properties.json,launch.json,tasks.json
在这里插入图片描述
然后分别复制下面的内容到这三个文件中

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",   /**** 修改成自己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",		/**** 修改成自己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",	/**** 修改成自己bin目录下的g++.exe,这里的路径和电脑里复制的文件目录有一点不一样,这里是两个反斜杠\\ ****/
        "args": [
            "-g",
            "${file}",
            "-o",
            "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "-I",
            "D:\\05cpp demo",      /**** 修改成自己放c/c++项目的文件夹,这里的路径和电脑里复制的文件目录有一点不一样,这里是两个反斜杠\\ ****/
            "-std=c++17"
        ],
        "options": {
            "cwd": "D:\\01Software\\MinGW\\bin"	/**** 修改成自己的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;
}

在这里插入图片描述

可以看到安装了插件之后,就可以调用g++.exe这个编译器了!!!很方便,不需要手动输入编译指令了
在这里插入图片描述

6.使用万能头文件 #include <bits/stdc++.h>

// C++ includes used for precompiling -*- C++ -*-

// Copyright (C) 2003-2018 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.

// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
// <http://www.gnu.org/licenses/>.

/** @file stdc++.h
 *  This is an implementation file for a precompiled header.
 */

// 17.4.1.2 Headers

// C
#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

// C++
#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
在这里插入图片描述

但是在 helloworld.cpp 中直接#include <bits/stdc++.h>是用不了的,编译会报错,这是为什么呢?
打开bits/stdc++.h,我们会看到
这个头文件最后面:和网上的万能头文件相比,多了一块儿这个声明,导致报错,删除这一块儿进行了
在这里插入图片描述

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

十六、windows11下VSCode配置C/C++编译环境 的相关文章

随机推荐

  • RT-thread 中CAN总线的应用

    准备 xff1a RT thread Studio 2 2 5 CubeMX 6 6 1 rt thread驱动包 4 0 3 1 新建项目 2 打开CubeMX Settings xff0c 设置CAN 找到CAN1 xff0c 并勾选激
  • Rt-thread的CAN应用2

    1 rtt中使能CAN1 2 CubeMX生成部分代码 xff0c 完成下面操作然后生成MDK ARM项目文件 3 将CubeMX生成的 HAL CAN MspInit 函数 粘贴到drv can c中 并在rt hw can init 函
  • STM32的IIC接口输入输出定义

    IO方向设置 PC11 端口 define SDA IN GPIOC gt CRH amp 61 0XFFFF0FFF GPIOC gt CRH 61 8 lt lt 12 define SDA OUT GPIOC gt CRH amp 6
  • 从头开始用树莓派做一个NAS【最新超详细教程】

    一 概述 众所周知在办公的时候两台电脑之间经常倒数据资料非常麻烦 xff0c 而NAS可以很好的解决这个问题 树莓派搭建NAS方法有很多 xff0c 我们之前也拍过直接用Samba FTP这些来实现NAS功能 xff0c 但是这些需要你会在
  • 串口发送通信---UART发送---STM32F4实现

    串口发送程序配置过程 xff08 HAL库 xff09 初始化串口相关参数 xff0c 使能串口 HAL StatusTypeDef span class token function HAL UART Init span span cla
  • 基于UDP的文件传输VS编程

    要求 xff1a 实现基于UDP的文件传输 xff0c 客户端发送文件 xff0c 服务端接受文件 服务端实现 xff1a include 34 stdafx h 34 include 34 iostream 34 include 34 a
  • 成功解决VSCODE运行C++的报错问题g++

    成功解决VSCODE运行C 43 43 的报错问题g 43 43 解决方案调试 很久写C 43 43 了 xff0c 直接打开C 43 43 写代码测试一下 xff0c 报错 xff0c 真一脸懵逼 捯饬很久 xff0c 解决了问题 解决方
  • linux下的环境变量

    环境变量 环境变量 xff0c 是指在操作系统中用来指定操作系统运行环境的一些参数 通常具备以下特征 xff1a 字符串 本质 有统一的格式 xff1a 名 61 值 值 值用来描述进程环境信息 存储形式 xff1a 与命令行参数类似 ch
  • 【004 关键字】extern “C“的作用是什么?

    一 extern 34 C 34 的作用 extern 34 C 34 关键字常用于C 43 43 和C混合编程中 xff0c 用于指定函数或变量采用C语言的命名和调用约定 加上extern 34 C 34 后 xff0c 会指示编译器这部
  • C++模板编程--学习C++类库的编程基础

    目录 一 函数模板二 类模板三 实现C 43 43 STL向量容器vector代码四 理解容器空间配置器allocator的重要性 一 函数模板 1 模板的意义 xff1a 对类型也可以进行参数化了 2 函数模板 lt 61 是不进行编译的
  • cmake 学习使用笔记(二)库的生成与使用

    学习使用cmake 生成及使用库 xff0c 使用cmake 生成 lib 静态库 及动态库 xff08 dll xff09 及linux 下使用的静态库 a 和 动态库 xff08 so 也称为共享库 xff09 目录 使用工具 生成库
  • cmake 学习使用笔记(五)手动编译

    目录 编译cmake 项目 构建 方式一 xff1a 可执行文件生成的在当前目录 方式二 xff1a 可执行文件生成只制定目录 记录一下手动编译 cmake 项目 编译cmake 项目 项目结构 注意 xff1a 必须要有一个 CMakeL
  • 四旋翼无人机学习第5节--STM32最小系统外围电路分析

    文章目录 1 芯片手册分析2 stm32的外部晶振手册分析2 stm32的外部上电复位手册分析3 电源放置方法4 GND放置方法5 其他元器件放置方法6 网络放置方法7 快捷键分享8 原理图绘制成果分享 快速使用芯片的最好的方式 xff0c
  • 现代C++单文件库(header only)

    nlohmann json https github com nlohmann json easyloggingpp https github com amrayn easyloggingpp
  • 一、ROS创建工作空间

    一 ROS创建工作空间 1 什么是工作空间2 创建工作空间3 编译工作空间4 创建功能包5 编译功能包 xff08 重新编译工作空间 xff09 6 设置环境变量7 运行节点 1 什么是工作空间 工作空间 xff08 workspace x
  • 二、ROS集成开发环境

    二 ROS集成开发环境 1 安装terminator2 安装VScode3 在VScode中使用ROS3 1 创建工作空间3 2 启动VScode3 3 配置VScode3 4 创建功能包3 5 编写C 43 43 文件3 6 配置CMak
  • 十一、Ubuntu18.04下VSCode配置C/C++编译环境

    十一 Ubuntu18 04下VSCode配置C C 43 43 编译环境 1 安装VSCode2 安装插件3 创建工程4 总结 1 安装VSCode 这一步就不谈了 2 安装插件 编译一些简单的cpp文件 xff0c 下面几个插件就够用了
  • 十五、Typora官方主题 + 自定义主题

    十五 Typora官方主题 43 自定义主题 1 下载官方主题2 在官方主题的基础上自定义 1 下载官方主题 首先去Typora官网下载自己喜欢的主题 xff1a 官方主题下载 例如我这里选择 Vue 我们只用到里面的两个主题文件vue c
  • linux---进程控制

    进程控制 fork函数 创建一个子进程 pid t fork void 失败返回 1 xff1b 成功返回 xff1a 父进程返回子进程的ID 非负 子进程返回 0 pid t vfork void 同样时创建一个子进程 xff0c 但是他
  • 十六、windows11下VSCode配置C/C++编译环境

    十六 windows11下VSCode配置C C 43 43 编译环境 1 安装VSCode2 中文插件3 MinGW编译器下载和配置4 VSCode配置c c 43 43 编译环境5 测试是否配置成功6 使用万能头文件 include l