How to translate paths from C:\... to /c/... in vs-code tasks? - visual-studio-code

I would like to build my program using Git Bash or MinGW so I have this build task:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "gcc.exe build active file",
"command": "gcc",
"args": ["-g", "${relativeFile}", "-o", "${fileBasenameNoExtension}"],
"options": {
"shell": {
"executable": "C:\\Program Files\\Git\\bin\\bash.exe",
"args": ["-l"]
},
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
},
]
}
I have two issues:
The cwd command does not work because ${fileDirname} is in the Windows form C:\Foo\Bar and since I am on bash I would like the POSIX form /c/Foo/Bar.
My task is not executed in the current terminal, but in a weird Window that display no output:
/usr/bin/bash: gcc -g .vscode\tasks.json -o tasks: No such file or directory
The terminal process terminated with exit code: 127
Terminal will be reused by tasks, press any key to close it.

With the extension Command Variable (v0.2.0) you can add Posix versions of the directory variables.
"cwd": "${command:extension.commandvariable.file.fileDirnamePosix}"

Related

How to open zsh terminals using vs code tasks in wsl?

My aim is to open zsh terminals in the current working directory of wsl whenever I open a workspace. For example, if I open a workspace with three folders, viz. test1, test2, and test3, I want one terminal in each directory to open in vs code.
For that purpose, I created .vscode/tasks.json file in each directory and input the following code:
{
"version": "2.0.0",
"tasks": [
{
"label": "test1",
"type": "shell",
"command": "",
"isBackground": true,
"problemMatcher": [],
"options": {
"shell": {
"executable": "zsh",
"args": []
}
},
"runOptions": {
"runOn": "folderOpen"
},
"presentation": {
"reveal": "always",
"panel": "dedicated"
}
}
]
}
But in the terminal, I get this error:
* Executing task in folder illuminate:
/usr/bin/zsh: can't open input file:
* The terminal process "zsh ''" failed to launch (exit code: 127).
* Terminal will be reused by tasks, press any key to close it.
I checked the docs, but it includes settings only useful for windows, not linux. Is there any way to achieve this?

Configuring a customized build task in vs code for c++

I had a custom build system in sublime for C++ which takes input from one file and redirects the output to another.
My sublime build system is as follows :
{
"cmd": ["g++.exe","-std=c++14", "${file}", "-o", "${file_base_name}.exe", "&&" , "${file_base_name}.exe<inputf.in>outputf.in"],
"selector":"source.cpp",
"shell":true,
"working_dir":"$file_path"
}
this helped in taking input from "inputf.in" file and print the output in "outputf.in" whenever i ran my program.
I want similar functionality in vs code but dont know how to configure build-task.
Here is a build task for a Node addon, you have to put it in .vscode/tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "Build",
"type": "shell",
"command": "npx node-pre-gyp build -j max",
"problemMatcher": {
"base": "$gcc",
"fileLocation": [
"relative",
"${workspaceFolder}/build"
]
},
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
You can press Ctrl-Shift-P to bring the command palette and select configure build task.
The problem matcher allows VSCode to highlight the compiler errors in your editor.
The default build task is the one you launch with a hotkey.

operate as https://code.visualstudio.com/docs/cpp/config-linux,but when Run Build Task ,show error

{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: /usr/bin/g++"
}
]
}
Starting build...
Build finished with error:
/usr/bin/x86_64-linux-gnu-ld:/home/chenxin/Documents/projects/helloworld/.vscode/tasks.json: file format not recognized; treating as linker script
/usr/bin/x86_64-linux-gnu-ld:/home/chenxin/Documents/projects/helloworld/.vscode/tasks.json:1: syntax error
collect2: error: ld returned 1 exit status
The terminal process failed to launch (exit code: -1).
Terminal will be reused by tasks, press any key to close it.
You are actually trying to compile a JSON file other than the cpp file you are using. You should first switch to the tab of the cpp code and then run the task or run the build task

Configuring launch.json, task.json and settings.json for debugging in VS Code with git bash as default terminal?

I am trying to configure the Debugger in VSCode
I looked through official documentation to setup the VSCode debugger for C/C++ but it's not working.
Documentation states the steps for setting up vscode debugger for
powershell in windows.
But I am trying to set-up debugger with git bash as my default integrated terminal in windows.
I have added git bash as default terminal but I am not able to setup debugger with git bash as integrated terminal.
Default configuration files :
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe build active file"
}
]
}
The property :
"externalConsole": false
is set to false as I want VSCode to use the integrated default bash terminal instead of using external terminal for debugging.
tasks.json :
{
"tasks": [
{
"type": "shell",
"label": "C/C++: g++.exe build active file",
"command": "C:\\MinGW\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
],
"version": "2.0.0"
}
c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "C:\\MinGW\\bin\\gcc.exe",
"cStandard": "c11",
"cppStandard": "gnu++14",
"intelliSenseMode": "clang-x86"
}
],
"version": 4
}
settings.json
{
"files.associations": {
"iostream": "cpp"
},
"C_Cpp.errorSquiggles": "Disabled"
}
With the above configuration, when I start the debugging it gives me the following error:
It seems like the command property in tasks.json is incorrect,
as bash converts
"C:\MinGW\bin\g++.exe" -> "C:MinGWbing++.exe"
and gives error: "no command found" because back-slash('\') in bash is an escape character.
Now changing above path in tasks.json to bash style :
"command": "C:/MinGW/bin/g++.exe"
resolves the above error, but now it gives same error for variable ${file} as this path-variable gets dynamically set to current open file .cpp.
I have been working on this debugging issue for last few days, but haven't found any workaround yet.
How can I change/update by configuration files to use git bash as default integrated terminal in VSCode for debugging.
It seems like the command property in tasks.json is incorrect, as bash converts
"C:\MinGW\bin\g++.exe" -> "C:MinGWbing++.exe"
Then try a cygwin-like path:
/c/MingW/bin/g++.exe
# or
/C/MingW/bin/g++.exe
Check if it is interpreted correctly by the git bash session then.

why tasks.json: file format not recognized

I run VScode on ubuntu16, and test the helloworld.cpp.
At first, I walked through all, then I tried to edit the tasks.json.
I got the error. Ok, I cancel all the the change with rollback, it shows error aagin.
I remove the helloworld folder and try again the helloworld.
After Terminal > Configure Default Build Task and build, the error shows again.
What's the wrong? It is got from vscode.
The error message:
Executing task: /usr/bin/g++ -g /home/liwenz/helloworld/.vscode/tasks.json -o /home/liwenz/helloworld/.vscode/tasks <
/usr/bin/ld:/home/liwenz/helloworld/.vscode/tasks.json: file format not recognized; treating as linker script
/usr/bin/ld:/home/liwenz/helloworld/.vscode/tasks.json:1: syntax error
collect2: error: ld returned 1 exit status
The terminal process terminated with exit code: 1
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
the helloworld link is https://code.visualstudio.com/docs/cpp/config-linux
You have to switch from the tasks.json file to the actual code you want to compile or debug.
As I build, I open tasks.json. So ${File}=tasks.json
I should open helloworld.cpp, then ${File}=helloword.cpp, and build ok.