Issue
WSL makes it easy to get gcc and other Linux tools. You can use a script to quickly configure a WSL development environment, so if you mess something up you can quickly destroy it and start over again with minimal cost. Thus I would like to run tasks on WSL instead of on Windows itself.
In Sublime3 for example, I can build C code using the WSL installation of GCC by simply creating a new build system with the following code:
{
"cmd" : ["bash", "-c", "gcc ${file_name} -o ${file_base_name} && ./${file_base_name}"],
"shell": true,
"working_dir": "${file_path}",
}
I have been trying to figure out how to replicate this on VS Code with no luck so far. It seems as if at least one other person has managed to get this working so it should be possible.
It seems that the proper way is to configure a tasks.json
file. The relevant lines seem to be:
"command": "bash.exe",
"args": ["-c", "'insert code here'"]
It seems that this tells the powershell to run bash -c 'insert code here'
. Can anyone come up with the right command?
Some Background:
${file}
is a a variable representing the active file in VSCode. Here is the list of all predefined variables.
wslpath
is a new bash command shipping in Windows 1803 which takes a Windows path and returns the wsl path e.g.:
$ wslpath "c:\Users\Evan\Google Drive\Development\Programming Languages\file.c"
/mnt/c/Users/Evan/Google Drive/Development/Programming Languages/file.c
My Attempts:
I tried writing a script to do it, but ran into issues. The tasks.json syntax to call myscript
should be something like this (note that we pass the file name to the script so that the script can pass it to GCC).
"args": ["-c", "'myscript.sh \"${file}\"'"]
or as per this answer
"command": "/path/to/script.sh"
"args": ["\"${file}\""]
myscript
might look something like this:
#!/bin/bash
echo "File Path is here $1"
x=$(wslpath "$1")
gcc "$x" -o a.out
./a.out
Solution
It can be done without the script:
"command": "gcc '$(wslpath '${file}')' && ./a.out"
${file}
would be parsed before the command parsed in bash.
Answered By - idanp Answer Checked By - Terry (WPSolving Volunteer)