VSCode运行C++代码

技术 · 03-05 · 165 人浏览

VSCode运行C++代码

  1. 下载C++扩展

    image-20240305124927671

  2. 滚到描述下面,点击 GCC and Mingw-w64 on Windows

    image-20240305125025597

  3. 点击 MSYS2

    image-20240305125112585

  4. 下载msys2-x86_64-20240113.exe

    image-20240305125251098

  5. 下载完成后,双击打开进行安装。跟随网页MSYS2上的步骤一步一步做。(建议安装路径不要更改选择默认路径)

    Second screen of MSYS2 installation

    Third screen of MSYS2 installation

  6. 安装完打开 MSYS2 MINGW64。首先更新系统环境:

    pacman -Syu
    • 更新好后回推出
    • 安装mingw gcc
    pacman -S mingw-w64-ucrt-x86_64-gcc
    • 写入Y,按下Enter
    • 安装完成后,输入:

      gcc --version
    • 获得以下结果

      image-20240305125910720

  7. 继续输入:

    pacman -S --needed base-devel mingw-w64-x86_64-toolchain
    • 出现Enter a selection (default=all):按下Enter继续
    • 出现:: Proceed with installation? [Y/n]写入Y,按下Enter
    • 安装完成后,关闭MSYS2 MINGW64
    • 添加环境变量

      • 在path中添加
      • C:\msys64\mingw64\bin
    • 打开cmd,依次输入:

      gcc --version
      g++ --version
      gdb --version

      image-20240305133204406

      显示以上内容即为安装成功。

  8. 打开vscode,Ctrl + Shift + P,输入 edit configurations,打开Edit Configurations (UI)

    image-20240305133305089

  9. Compiler path 改为 C:/msys64/mingw64/bin/g++.exe (这就是为什么建议选择默认路径安装,不然容易找不到)

    image-20240305133725565

  10. IntelliSense mode 改为 windows-gcc-64

    image-20240305133826565

  11. 测试是否成功,在项目文件夹下新建一个.cpp文件,输入如下代码

    #include <iostream>
    using namespace std;
     
    int main()
    {
        cout << "Hello World!" << endl;
        return 0;
    }
    • 点击运行的三角形图标,显示运行成功。

    image-20240305134852097

VSCode C++
Theme Jasmine by Kent Liao