Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Windows Subsystem for Linux (WSL) from Sublime Text

I wanted to use gcc, g++ and make from Sublime to be able to compile c and c++ codes to Linux runnables on Winows. I couldn't run bash.exe from Sublime, as many other users on stackoverflow.

like image 578
Fontinalis Avatar asked Nov 06 '16 23:11

Fontinalis


People also ask

Can I use Windows Subsystem for Linux?

You can now install everything you need to run Windows Subsystem for Linux (WSL) by entering this command in an administrator PowerShell or Windows Command Prompt and then restarting your machine. This command will enable the features necessary to run WSL and install the Ubuntu distribution of Linux.

Can I install WSL2 without Hyper V?

While WSL 2 uses Microsoft's Hyper-V as a hypervisor under the hood to run the utility VM, it does not require you to enable Windows' Hyper-V role or feature; WSL works perfectly fine without it.

How do I install Subsystem for Linux WSL on Windows 11?

Step 1: Start CMD with administrative privileges. Step 2:Execute "wsl --install" command. Step 3:Run "wsl -l -o" to list other Linux releases. Step 4:You can install your favorite Linux distribution, use "wsl --install -d NameofLinuxDistro."


2 Answers

  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"

like image 63
Fontinalis Avatar answered Sep 18 '22 14:09

Fontinalis


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.

like image 43
rkwap Avatar answered Sep 18 '22 14:09

rkwap