下载、安装和设置 Linux 工作负载Download, install, and set up the Linux workload

Visual Studio 2017 及更高版本支持 Linux 项目。Linux projects are supported in Visual Studio 2017 and later. 若要查看这些版本的文档,请将本文的 Visual Studio“版本” 选择器控件设置为 Visual Studio 2017 或 Visual Studio 2019。To see the documentation for these versions, set the Visual Studio Version selector control for this article to Visual Studio 2017 or Visual Studio 2019. 它位于此页面上目录表的顶部。It's found at the top of the table of contents on this page.

可以使用 Windows 中的 Visual Studio IDE 来创建、编辑和调试在远程 Linux 系统、虚拟机或适用于 Linux 的 Windows 子系统上执行的 C++ 项目。You can use the Visual Studio IDE on Windows to create, edit, and debug C++ projects that execute on a remote Linux system, virtual machine, or the Windows Subsystem for Linux.

可处理使用 CMake 的现有基本代码,无需将其转换为 Visual Studio 项目。You can work on your existing code base that uses CMake without having to convert it to a Visual Studio project. 如果基本代码为跨平台代码,则从 Visual Studio 中可同时面向 Windows 和 Linux。If your code base is cross-platform, you can target both Windows and Linux from within Visual Studio. 例如,可以使用 Visual Studio 在 Windows 上编辑、生成和调试代码。For example, you can edit, build, and debug your code on Windows using Visual Studio. 然后快速使项目重新面向 Linux 以在 Linux 环境中生成和调试。Then, quickly retarget the project for Linux to build and debug in a Linux environment. Linux 头文件会自动复制到本地计算机。Linux header files are automatically copied to your local machine. Visual Studio 会使用这些文件提供完整 IntelliSense 支持(“语句完成”、“转到定义”等)。Visual Studio uses them to provide full IntelliSense support (Statement Completion, Go to Definition, and so on).

对于任何这些方案,都必须拥有“使用 C++ 的 Linux 开发”工作负载 。For any of these scenarios, the Linux development with C++ workload is required.

Visual Studio 安装程序Visual Studio setup

  1. 在 Windows 搜索框中键入“Visual Studio 安装程序”:Type "Visual Studio Installer" in the Windows search box:

    Windows 搜索框

  2. 在“应用”结果下寻找安装程序并双击它 。Look for the installer under the Apps results and double-click it. 打开该安装程序后,选择“修改” ,然后单击“工作负荷” 选项卡。向下滚动到“其他工具集” ,然后选择 “使用 C++ 的 Linux 开发”工作负荷。When the installer opens, choose Modify, and then click on the Workloads tab. Scroll down to Other toolsets and select the Linux development with C++ workload.

    适用于 Linux 开发的 Visual C++ 工作负荷

  3. 如果你面向的是 IoT 或嵌入式平台,请转到右侧的“安装详细信息” 窗格。If you're targeting IoT or embedded platforms, go to the Installation details pane on the right. 在“使用 C++ 的 Linux 开发” 下,展开“可选组件” ,并选择所需的组件。Under Linux development with C++, expand Optional Components, and choose the components you need. 默认情况下,选择适用于 Linux 的 CMake 支持。CMake support for Linux is selected by default.

  4. 单击“修改” 以继续进行安装。Click Modify to continue with the installation.

用于创建 Linux 环境的选项Options for creating a Linux environment

如果还没有 Linux 计算机,则可以在 Azure 上创建 Linux 虚拟机。If you don't already have a Linux machine, you can create a Linux Virtual Machine on Azure. 有关详细信息,请参阅快速入门:在 Azure 门户中创建 Linux 虚拟机For more information, see Quickstart: Create a Linux virtual machine in the Azure portal.

在 Windows 10 上,可以在适用于 Linux 的 Windows 子系统 (WSL) 上安装你最喜欢的 Linux 发行版以及使该发行版以之为目标。On Windows 10, you can install and target your favorite Linux distro on the Windows Subsystem for Linux (WSL). 有关详细信息,请参阅 Windows 10 的适用于 Linux 的 Windows 子系统安装指南For more information, see Windows Subsystem for Linux Installation Guide for Windows 10. 如果无法访问 Windows 应用商店,可以手动下载 WSL 发行包If you're unable to access the Windows Store, you can manually download the WSL distro packages. WSL 是一种方便的控制台环境,但不建议用于图形应用程序。WSL is a convenient console environment, but it's not recommended for graphical applications.

Visual Studio 中的 Linux 项目要求在远程 Linux 系统或 WSL 上安装以下依赖项:Linux projects in Visual Studio require the following dependencies to be installed on your remote Linux system or WSL:

  • 编译器 - Visual Studio 2019 为 GCC 和 Clang 提供完整支持。A compiler - Visual Studio 2019 has full support for GCC and Clang.
  • gdb - Visual Studio 会在 Linux 系统上自动启动 gdb,并使用 Visual Studio 调试器的前端在 Linux 上提供完全保真的调试体验。gdb - Visual Studio automatically launches gdb on the Linux system, and uses the front end of the Visual Studio debugger to provide a full-fidelity debugging experience on Linux.
  • rsync 和 zip - 包含 rsync 和 zip 允许 Visual Studio 将头文件从 Linux 系统提取到 Windows 文件系统以供 IntelliSense 使用。rsync and zip - the inclusion of rsync and zip allows Visual Studio to extract header files from your Linux system to the Windows filesystem for use by IntelliSense.
  • makemake
  • openssh-server (仅适用于远程 Linux 系统)- Visual Studio 通过安全 SSH 连接以连接到远程 Linux 系统。openssh-server (remote Linux systems only) - Visual Studio connects to remote Linux systems over a secure SSH connection.
  • CMake(仅 CMake 项目)- 可以为 Linux 安装 Microsoft 的静态链接的 CMake 二进制文件CMake (CMake projects only) - You can install Microsoft's statically linked CMake binaries for Linux.
  • ninja-build (仅 CMake 项目)- Ninja 是 Visual Studio 2019 版本 16.6 或更高版本中适用于 Linux 和 WSL 配置的默认生成器。ninja-build (CMake projects only) - Ninja is the default generator for Linux and WSL configurations in Visual Studio 2019 version 16.6 or later.

以下命令假设你使用的是 g++ 而非 clang。The following commands assume you're using g++ instead of clang.

Visual Studio 中的 Linux 项目要求在远程 Linux 系统或 WSL 上安装以下依赖项:Linux projects in Visual Studio require the following dependencies to be installed on your remote Linux system or WSL:

  • gcc - Visual Studio 2017 为 GCC 提供完整支持。gcc - Visual Studio 2017 has full support for GCC.
  • gdb - Visual Studio 会在 Linux 系统上自动启动 gdb,并使用 Visual Studio 调试器的前端在 Linux 上提供完全保真的调试体验。gdb - Visual Studio automatically launches gdb on the Linux system and uses the front end of the Visual Studio debugger to provide a full-fidelity debugging experience on Linux.
  • rsync 和 zip - 包含 rsync 和 zip 允许 Visual Studio 将头文件从 Linux 系统提取到 Windows 文件系统以供 IntelliSense 使用。rsync and zip - the inclusion of rsync and zip allows Visual Studio to extract header files from your Linux system to the Windows filesystem to use for IntelliSense.
  • makemake
  • openssh-server - Visual Studio 通过安全 SSH 连接以连接到远程 Linux 系统。openssh-server - Visual Studio connects to remote Linux systems over a secure SSH connection.
  • CMake(仅 CMake 项目)- 可以为 Linux 安装 Microsoft 的静态链接的 CMake 二进制文件CMake (CMake projects only) - You can install Microsoft's statically linked CMake binaries for Linux.

Linux 安装程序:WSL 上的 UbuntuLinux setup: Ubuntu on WSL

以 WSL 为目标时,无需添加远程连接或配置 SSH 即可进行生成和调试。When you're targeting WSL, there's no need to add a remote connection or configure SSH to build and debug. 使用 Visual Studio for Intellisense 支持自动同步 Linux 标头需要 zip 和 rsync 。zip and rsync are required for automatic syncing of Linux headers with Visual Studio for Intellisense support. 仅 CMake 项目需要 ninja-build 。ninja-build is only required for CMake projects. 如果所需应用程序尚不存在,则可以使用以下命令进行安装:If the required applications aren't already present, you can install them using this command:

sudo apt-get install g++ gdb make ninja-build rsync zip

远程 Linux 系统上的 UbuntuUbuntu on remote Linux systems

目标 Linux 系统必须安装 openssh-server、g++、gdb 和 make 。The target Linux system must have openssh-server, g++, gdb, and make installed. 仅 CMake 项目需要 ninja-build 。ninja-build is required for CMake projects only. ssh 守护程序必须正在运行。The ssh daemon must be running. 需要 zip 和 rsync 才能自动将远程标头与本地计算机同步以获得 Intellisense 支持。zip and rsync are required for automatic syncing of remote headers with your local machine for Intellisense support. 如果这些应用程序尚不存在,则可以按如下所述进行安装:If these applications aren't already present, you can install them as follows:

  1. 在 Linux 计算机上的 shell 提示符下,运行:At a shell prompt on your Linux computer, run:

    sudo apt-get install openssh-server g++ gdb make ninja-build rsync zip
    

    可能会提示你输入 root 密码以运行 sudo 命令。You may be prompted for your root password to run the sudo command. 如果是这样,输入密码然后继续。If so, enter it and continue. 完成后,可安装所需服务和工具。Once complete, the required services and tools are installed.

  2. 通过运行以下命令,确保 ssh 服务在 Linux 计算机上运行:Ensure the ssh service is running on your Linux computer by running:

    sudo service ssh start
    

    此命令将启动该服务并在后台运行它,准备接受连接。This command starts the service and runs it in the background, ready to accept connections.

WSL 上的 FedoraFedora on WSL

Fedora 使用 dnf 包安装程序 。Fedora uses the dnf package installer. 要下载 g++、gdb、make、rsync、ninja-build 和 zip,请运行 :To download g++, gdb, make, rsync, ninja-build, and zip, run:

sudo dnf install gcc-g++ gdb rsync ninja-build make zip

使用 Visual Studio for Intellisense 支持自动同步 Linux 标头需要 zip 和 rsync 。zip and rsync are required for automatic syncing of Linux headers with Visual Studio for Intellisense support. 仅 CMake 项目需要 ninja-build 。ninja-build is only required for CMake projects.

远程 Linux 系统上的 FedoraFedora on remote Linux systems

运行 Fedora 的目标计算机使用 dnf 包安装程序 。The target machine running Fedora uses the dnf package installer. 要下载 openssh-server、g++、gdb、make、ninja-build、rsync 和 zip 并重启 ssh 守护程序,请遵循以下说明 。To download openssh-server, g++, gdb, make, ninja-build, rsync, and zip, and restart the ssh daemon, follow these instructions. 仅 CMake 项目需要 ninja-build 。ninja-build is only required for CMake projects.

  1. 在 Linux 计算机上的 shell 提示符下,运行:At a shell prompt on your Linux computer, run:

    sudo dnf install openssh-server gcc-g++ gdb ninja-build make rsync zip
    

    可能会提示你输入 root 密码以运行 sudo 命令。You may be prompted for your root password to run the sudo command. 如果是这样,输入密码然后继续。If so, enter it and continue. 完成后,可安装所需服务和工具。Once complete, the required services and tools are installed.

  2. 通过运行以下命令,确保 ssh 服务在 Linux 计算机上运行:Ensure the ssh service is running on your Linux computer by running:

    sudo systemctl start sshd
    

    此命令将启动该服务并在后台运行它,准备接受连接。This command starts the service and runs it in the background, ready to accept connections.

后续步骤Next Steps

现在,可以创建或打开 Linux 项目并将其配置为在目标系统上运行。You're now ready to create or open a Linux project and configure it to run on the target system. 有关详细信息,请参见:For more information, see: