前言

本篇文章在CodeGeeX的辅助下编写
最近在学C语言,在VS Code上配置C语言编译环境,记录一下。
笔者是根据微软官方的教程来配置的,所以教程中的部分内容可能与网上的不同。
使用此教程请保证网络通畅,笔者的网络环境为校园网,故在配置过程中遇到问题,请检查网络环境。

安装 C/C++ 扩展

在扩展商店中搜索 C/C++ 并安装。

以上都是废话,笔者懒,还是翻译官方文档算了


原文链接 :C/C++ for Visual Studio Code

C/C++ for Visual Studio Code / 用于 Visual Studio Code 的 C/C++

C/C++ support for Visual Studio Code is provided by a Microsoft C/C++ extension to enable cross-platform C and C++ development on Windows, Linux, and macOS. When you create a *.cpp file, the extension adds features such as syntax highlighting (colorization), smart completions and hovers (IntelliSense), and error checking.

微软 Visual Studio Code C/C++ 扩展为 Windows、Linux 和 macOS 上的跨平台 C 和 C++ 开发提供了支持。当你创建一个 .cpp 文件时,扩展会添加诸如语法高亮(着色)、智能补全和悬停(IntelliSense)以及错误检查等功能。

Install the extension / 安装扩展

  1. Open VS Code. / 打开 VS Code
  2. Select the Extensions view icon on the Activity bar or use the keyboard shortcut (Ctrl+Shift+X). / 点击活动栏中的扩展视图图标或使用快捷键(Ctrl+Shift+X
  3. Search for C++. / 搜索 C++
  4. Select Install. / 选择安装

Set up your C++ Environment / 设置 C++ 环境

C++ is a compiled language meaning your program’s source code must be translated (compiled) before it can be run on your computer. The C/C++ extension doesn’t include a C++ compiler or debugger, since VS Code as an editor relies on command-line tools for the development workflow. You need to install these tools or use the tools already installed on your computer.

C++ 是一种编译型语言,这意味着程序源代码必须先翻译(编译)才能在计算机上运行。C/C++ 扩展不会包含 C++ 编译器或调试器,因为 VS Code 作为编辑器依赖于命令行工具进行开发工作流。你需要安装这些工具或使用计算机上已安装的工具。

Check if you have a compiler installed / 检查是否已安装编译器

Note: There may already be a C++ compiler and debugger provided by your academic or work development environment. Check with your instructors or colleagues for guidance on installing the recommended C++ toolset (compiler, debugger, project system, linter).

注意:你可能已经从学习或工作开发环境中获得了一个 C++ 编译器与调试器。请与你的导师或同事联系以获取推荐的 C++ 工具集(编译器、调试器、项目系统、Linter)的安装指南。

Common compilers that already come preinstalled on some platforms are the GNU Compiler Collection (GCC) on Linux and the Clang tools with Xcode on macOS. / 常见平台上的预安装编译器是 Linux 上的 GNU 编译器集合(GCC)和 macOS 上的 Clang 工具。

To check if you already have them installed: / 检查是否已安装它们:

  1. Open a new VS Code terminal window using (Ctrl+Shift+`) / 打开一个新 VS Code 终端窗口(Ctrl+Shift+`)。

  2. Use the following command to check for the GCC compiler g++: / 使用以下命令检查 GCC 编译器 g++

    1
    g++ --version

    Or this command for the Clang compiler clang: / 或者这个命令检查 Clang 编译器 clang

    1
    clang --version

The output should show you the compiler version and details. If neither are found, make sure your compiler executable is in your platform path (%PATH on Windows, $PATH on Linux and macOS) so that the C/C++ extension can find it. Otherwise, use the instructions in the section below to install a compiler.

输出应显示编译器的版本和详细信息。如果没有找到,请确保编译器可执行文件已添加到平台路径(Windows 上为 %PATH,Linux 上为 $PATH,macOS 上为 %PATH)。否则,请使用下面的部分中的安装编译器说明。

Install a compiler / 安装编译器

If you don’t have a compiler installed, you can follow one of our installation tutorials: / 如果尚未安装编译器,可以遵循我们的安装教程:

Windows:

MSVC 教程
MinGW 教程

Linux:

GCC 教程

macOS:

Clang 教程

Note: If you would prefer a full Integrated Development Environment (IDE), with built-in compilation, debugging, and project templates (File > New Project), there are many options available, such as the Visual Studio Community edition.

注意:如果您想使用内置编译、调试和项目模板(文件 > 新建项目)功能的集成开发环境,则有很多软件可选,例如 Visual Studio Community

Example: Install MinGW-x64 on Windows / 例子:在 Windows 上安装 MinGW-x64

To understand the process, let’s install Mingw-w64 via MSYS2. Mingw-w64 is a popular, free toolset on Windows. It provides up-to-date native builds of GCC, Mingw-w64, and other helpful C++ tools and libraries.

为了理解过程,让我们通过 MSYS2 安装 Mingw-w64。Mingw-w64 是 Windows 上流行的免费工具集。它提供了最新的本地构建的 GCC、Mingw-w64 和其他有用的 C++ 工具和库。

  1. Download using this direct link to the MinGW installer. / 使用此直接链接下载 MinGW 安装程序

  2. Run the installer and follow the steps of the installation wizard. Note, MSYS2 requires 64 bit Windows 8.1 or newer. / 运行安装程序并按照安装向导中的步骤进行操作。请注意,MSYS2 要求 Windows 8.1 或更高版本的 64 位 Windows。

  3. In the wizard, choose your desired Installation Folder. Record this directory for later. In most cases, the recommended directory is acceptable. The same applies when you get to setting the start menu shortcuts step. When complete, ensure the Run MSYS2 now box is checked and select Finish. A MSYS2 terminal window will then automatically open. / 在向导中,选择所需的安装目录。记录此目录以供以后使用。在大多数情况下,建议的目录是可接受的。当完成设置启动菜单快捷方式时,请确保选择“现在运行 MSYS2”框,然后选择完成。然后,将自动打开 MSYS2 终端窗口。

  4. In this terminal, install the MinGW-w64 toolchain by running the following command: / 在此终端中,通过运行以下命令安装 MinGW-w64 工具链:

    1
    pacman -S --needed base-devel mingw-w64-ucrt-x86_64-toolchain
  5. Accept the default number of packages in the toolchain group by pressing Enter. / 接受默认的工具链组中软件包的数量,按 Enter 键。

  6. Enter Y when prompted whether to proceed with the installation. / 提示是否继续安装时,输入 Y

  7. Add the path to your MinGW-w64 bin folder to the Windows PATH environment variable by using the following steps: /
    使用以下步骤将 MinGW-w64 bin 文件夹的路径添加到 Windows PATH 环境变量:

    1. In the Windows search bar, type Settings to open your Windows Settings. /
      在 Windows 搜索栏中,键入 设置 打开 Windows 设置。
    2. Search for Edit environment variables for your account. /
      搜索 编辑你的账户的环境变量
    3. In your User variables, select the Path variable and then select Edit. /
      在用户变量中,选择 Path 变量,然后选择 编辑
    4. Select New and add the MinGW-w64 destination folder you recorded during the installation process to the list. If you selected the default installation steps, the path is: C:\msys64\ucrt64\bin. /
      选择 新建,并添加安装过程中记录的 MinGW-w64 目标文件夹到列表中。如果你选择了默认的安装步骤,路径是:C:\msys64\ucrt64\bin。
    5. Select OK to save the updated PATH. For the new PATH to be available, reopen your console windows. /
      选择 确定 保存更新后的 PATH。新的 PATH 生效需要重新打开控制台窗口。
  8. Check that your MinGW-w64 tools are correctly installed and available, open a new Command Prompt and type: /
    检查 MinGW-w64 工具是否正确安装,打开一个新的命令窗口,输入:

    1
    2
    3
    gcc --version
    g++ --version
    gdb --version

    You should see output that states which versions of GCC, g++ and GDB you have installed. If this is not the case, make sure your PATH entry matches the Mingw-w64 binary location where the compiler tools are located or reference the troubleshooting section.

你应该看到输出,显示出你安装的 GCC、g++ 和 GDB 的版本。如果不是,请确保 PATH 入口与编译器工具所在位置的 Mingw-w64 二进制文件匹配,或者参考故障排除部分


好了,笔者摸鱼回来了

配置任务

运行生成任务