VsCode中C/C++语言的编译运行


VsCode中如何编写C/C++语言?
这在VsCode 官网指南中有详细的指导,如果在本文中有不清楚内容,推荐你阅读一番。

1. 准备工作

(1)VsCode安装

VsCode下载官网: Vscode下载

(2)MinGW安装

  1. MinGW下载地址:MinGW下载
  2. MinGW安装好后需要配置Path,配置好后检查安装是否成功。
  3. 如果有 不确定安装选项、不知道怎么配置Path,可以查阅百度。

2.在VsCode中新建项目

(1)新建文件

  1. 在电脑中你喜欢的地方新建一个文件文件夹,比如Hello文件夹,然后在code中打开此文件夹,如图
    在这里插入图片描述
  2. 打开文件夹后,新建hello.cpp文件,在其中随便写入你喜欢的代码,如:
#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
    vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};

    for (const string& word : msg)
    {
        cout << word << " ";
    }
    cout << endl;
}

(2) 编译运行

  1. 配置文件
    点击终端—>配置默认生成任务,选择C/C++: g++.exe build active file
    选择好后,系统会在Hello文件夹下新建一个.vscode文件夹以及文件夹下面的tasks.json文件,里面有类似下面的代码:
{
  "version": "2.0.0",
  "tasks": [
    {
      "type": "shell",
      "label": "g++.exe build active file",
      "command": "C:\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\bin\\g++.exe",
      "args": ["-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe"],
      "options": {
        "cwd": "C:\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\bin"
      },
      "problemMatcher": ["$gcc"],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ]
}
  1. 编写运行

回到hello.cpp文件下,按住ctrl+shift+B,在终端面板中有如下命令:
在这里插入图片描述 并且在Hello文件下生成了hello.exe,编译成功!

接下来查看运行结果了:
点击终端面板右侧的“+”,选择在powershell终端下
在这里插入图片描述
在命令行中输入如下命令:

./hello.exe

就可以得到结果(附图)

在这里插入图片描述

行文至此,点个赞呗

猜你喜欢

转载自blog.csdn.net/weixin_46185572/article/details/106973259