5

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?

closed as off-topic by dobey, muru, Charles Green, Eric Carvalho, mniess Oct 20 '17 at 11:19

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This is not about Ubuntu. Questions about other Linux distributions can be asked on Unix & Linux, those about Windows on Super User, those about Apple products on Ask Different and generic programming questions on Stack Overflow." – dobey, muru, Charles Green, Eric Carvalho, mniess
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 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). – dobey Oct 19 '17 at 13:20
  • 1
    @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. – Briefkasten Oct 19 '17 at 16:26
4

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.