Fontinalis
Fontinalis

Reputation: 590

Using Windows Subsystem for Linux (WSL) from Sublime Text

I wanted to use gcc, g++ and make from Sublime Text to be able to compile C and C++ code to Linux runnables on Windows. I couldn't run bash.exe from Sublime Text, as many other users on Stack Overflow.

Upvotes: 13

Views: 18916

Answers (6)

Manoj
Manoj

Reputation: 1

This method worked for me

{
    "cmd": ["wsl", "g++", "`wslpath -u \"${file}\"`", "&&", "wsl", "./a.out"],
    "selector": "source.c++, source.cpp",
    "file_regex": "^(.*):(\\d+):(\\d+): error: (.*)$",
    "shell": true
}

Upvotes: 0

Manoj
Manoj

Reputation: 1

if above method is not working then try this, "e/CP" => this is my directory path in windows (E:\CP)

{
    "shell_cmd": "wsl g++ /mnt/e/CP/${file_base_name}.cpp && wsl ./a.out",
    "selector": "source.c++, source.cpp",
    "file_regex": "^(.*):(\\d+):(\\d+): error: (.*)$"
}

Upvotes: 0

CodeNStuff
CodeNStuff

Reputation: 314

This MS dev blog recommends wsl as the default way for invoking WSL command line calls from Windows. So, in principle your sublime build config should look like this:

{
    "shell_cmd": "wsl -- <your-linux-build-command>",
    "shell": true,
}

One caveat is the correct way of converting Windows paths to Linux style, which requires wslpath to be invoked as a sub-command in your call. E.g.:

{
    "shell_cmd": "wsl -- cmake --build \\$(wslpath '$project_path\\build')",
    "shell": true,
}

Mind that you need two \ to avoid sublime's variable replacement.

This works (at least for me) for WSL 1 and 2 alike.

Upvotes: 1

rkwap
rkwap

Reputation: 146

In WSL2, the best possible way according to me is using the below sublime-build file.

  • You have to create a new build system in the Sublime Text with the following code.
    (Tools -> Build System -> New Build System...)
    {
    "shell_cmd": "ubuntu run \"g++ `wslpath '${file}'` && ./a.out<inp.in>out.in \" ",
    "shell":true,
    "working_dir":"$file_path",
    "selector":"$file_name"
    }
  • This code will complile the .cpp code and use inp.in and out.in as input and output files respectively (Optional, if you don't want that, then replace ./a.out<inp.in>out.in with ./a.out). The output will be shown in the Sublime's Build Results panel.

  • When you want to use this Build System, select it in the Tools -> Build System list, then hit Ctrl + B.

Upvotes: 3

madhat1
madhat1

Reputation: 704

On WSL 2 the suggested solution doesn't work. Here is a solution to execute on a WSL 2 target a script edited in Sublime Text on Windows. Create a bash-wsl.sublime-build file:

{
    "shell_cmd": "bash -c \"wslpath '${file}'\" | bash -s",
    "shell": true,
}

Upvotes: 1

Fontinalis
Fontinalis

Reputation: 590

  1. You have to copy the C:\Windows\System32\bash.exe file to the C:\Windows\SysWOW64\ directory. Required because of the WoW64 file system redirection (Thanks Martin!)

  2. Then you have to create a new build system in the Sublime Text with the following code. (Tools -> Build System -> New Build System...)

    {
      "cmd" : ["bash", "-c", "gcc ${file_name} -o ${file_base_name} && ./${file_base_name}"],
      "shell": true,
      "working_dir": "${file_path}",
    }

    This code will complile the .c code and than run it. The output will be shown in the Sublime's Build Results panel.

  3. When you want to use this Build System, select it in the Tools -> Build System list, then hit Ctrl + B.

You can customize the command I put there, the main thing is that you can run Linux commands using bash -c "CommandsYouWantToRun"

Upvotes: 11

Related Questions