Use gcc from Windows Sublinuxsystem in Vscode on Windows

7,048

Visual Studio Code is a cross-platform IDE that uses a tasks.json file to describe how to compile (and perform other tasks) your project. You can run the VSCode in the Windows or in the WSL Ubuntu subsystem. If you wanna run it into the WSL, you must use the Ubuntu/Linux binaries.


Notes on WSL

In the Windows command line, you can run linux commands using bash

C:\> bash.exe -c <linux command>

For instance, you can run the Linux gcc using

C:\> bash -c "gcc -v"

If you have installed multiple linux systems on WSL, e.g. an opensuse and a ubuntu, you must use opensuse-42 run or ubuntu run instead of bash to determine which linux subsystem use at the windows command line.

C:\> ipconfig | opensuse-42 run grep IP | ubuntu run lolcat

In addition, note that the Windows file system can be accessed using /mnt/<drive letter>/. For instance, if you have a C:\Projects folder, you can access it from Linux at /mnt/C/Projects


Configuring a VSCode in Windows to use the GCC in the WSL Linux

Check the instructions in the website. To use GCC or CLang compilers in Mac or Linux, you may use the bash with different arguments depending on the task.

You can configure (or create) your own task.json. You must define the bash as the command to use. I think almost the same configuration you use for Mac/Linux must work. I changed the "cwd" option.

{
    // 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
            }
        ]
    }
}

There are some other Gists you can use as other examples.

NOTE: I have found some MS tutorials on compiling and debugging Linux GCC programs using WSL and Visual Studio but not Visual Studio Code.

Share:
7,048

Related videos on Youtube

Briefkasten
Author by

Briefkasten

Updated on September 18, 2022

Comments

  • Briefkasten
    Briefkasten over 1 year

    Since the Windows Fall Creators Update we can install a Linux subsystem like Ubuntu from the windows store on a windows machine.

    Is it possible to use the gcc compiler from the Linux subsystem on windows to build c applications with vscode on windows for linux?

    • dobey
      dobey over 6 years
      I've voted to close this as off topic, as this is more about features in VS Code on Windows, than it is a question about Ubuntu. GCC can obviously compile code, so the question is whether VS Code in Windows can expose your source tree to the container, and execute processes within it, to compile the code. This would be better asked on a Windows focused site, I think (though I suspect the answer is that this is unlikely to be doable).
    • Briefkasten
      Briefkasten over 6 years
      @dobey I was unsure too, where I should ask this kind of question. Because the main question is related to "how can I use gcc from wsl on windows" which is a feature of microsoft.com/en-us/store/p/ubuntu/9nblggh4msv6 and the support website refers to askubuntu I though it should be placed here.