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.
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.
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.
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."
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!)
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.
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"
In WSL2, the best possible way according to me is using the below sublime-build file.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With