在 Windows 上的 Vscode 中使用来自 Windows Sublinuxsystem 的 gcc

Bri*_*ten 5 windows gcc windows-subsystem-for-linux

自 Windows Fall Creators Update 以来,我们可以在 Windows 机器上从Windows 商店安装像 Ubuntu 这样的 Linux 子系统。

是否可以在 windows 上使用 Linux 子系统中的 gcc 编译器在 windows for linux 上使用 vscode 构建 c 应用程序?

Jai*_*ime 4

Visual Studio Code是一个跨平台 IDE,它使用tasks.json文件来描述如何编译(和执行其他任务)项目。您可以在 Windows 或 WSL Ubuntu 子系统中运行 VSCode。如果您想在 WSL 中运行它,则必须使用 Ubuntu/Linux 二进制文件。


WSL 注释

在 Windows 命令行中,您可以使用以下命令运行 linux 命令bash

C:\> bash.exe -c <linux command>
Run Code Online (Sandbox Code Playgroud)

例如,您可以gcc使用以下命令运行 Linux

C:\> bash -c "gcc -v"
Run Code Online (Sandbox Code Playgroud)

如果您在 WSL 上安装了多个 Linux 系统,例如 opensuse 和 ubuntu,则必须使用opensuse-42 runubuntu runbash确定在 Windows 命令行中使用哪个 linux 子系统。

C:\> ipconfig | opensuse-42 run grep IP | ubuntu run lolcat
Run Code Online (Sandbox Code Playgroud)

此外,请注意,可以使用 访问 Windows 文件系统/mnt/<drive letter>/。例如,如果您有一个C:\Projects文件夹,您可以从 Linux 访问它:/mnt/C/Projects


在 Windows 中配置 VSCode 以在 WSL Linux 中使用 GCC

检查网站中的说明。要在 Mac 或 Linux 中使用 GCC 或 CLang 编译器,您可以bash根据任务使用不同的参数。

您可以配置(或创建)您自己的task.json. 您必须将 定义bash为要使用的命令。我认为您在 Mac/Linux 上使用的配置几乎相同。我更改了“cwd”选项。

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "0.1.0",
    "wsl": {
        "command": "bash",
        "args": ["-c"],
        "isShellCommand": true,
        "showOutput": "always",
        "suppressTaskName": true,
        "options": {
            "cwd": "/mnt/C/${workspaceRoot}"
         },
        "tasks": [
             {
                "taskName": "hello",
                "args": [
                    "make hello"
                ],
                "isBuildCommand": true
             },
             {
                "taskName": "clean",
                "args": [
                    "make clean"
                ]
             },
             {
                "taskName": "compile w/o makefile",
                "args": [
                    "g++ helloworld.C -o hello"
                ],
                "echoCommand": true
            }
        ]
    }
}
Run Code Online (Sandbox Code Playgroud)

您还可以使用一些其他要点作为其他示例。

注意:我发现了一些关于使用 WSL 和 Visual Studio 编译和调试 Linux GCC 程序的MS 教程,但没有找到 Visual Studio Code。