VSCode: Code Runner extension is unable to execute the code on git bash terminal?

天涯浪子 提交于 2021-02-19 05:55:11

问题


I am trying to run a simple program "primeRange.cpp" using Code Runner extension available for VSCode.

I have selected my default terminal as git bash in VSCode, but when I hit Run on top right corner, it sends the command to bash to run the program using g++ compiler, but I am getting error as no such file or directory, when there exists a directory with the given name.

How can I fix this?

How can I customize which command to hit on bash when I press run on code runner?

I want to set the command as :

cd "c:\\Users\\Tushar\\Desktop\\contests\\Practice" && g++ primeRange.cpp -o primeRange && "c:\\Users\\Tushar\\Desktop\\contests\\Practice\\primeRange"

OR

cd "c:\Users\Tushar\Desktop\contests\Practice" && g++ primeRange.cpp -o primeRange && "c:\Users\Tushar\Desktop\contests\Practice\primeRange"

If I am executing any one of the above command manually on bash then it's working.

So I basically want to know that how to include the executable filename inside the path as:

"c:\Users\Tushar\Desktop\contests\Practice\primeRange"

instead of after the quotes as :

"c:\Users\Tushar\Desktop\contests\Practice\"primeRange

This is my settings.json.

I have updated the path in settings.json to:

"code-runner.executorMap": {
        "cpp": "cd $dir && g++ -std=c++11 $fileName -o $fileNameWithoutExt && \"./$fileNameWithoutExt.exe\""
    },

But now I am getting prompt instead of asking for stdin as given below:

RESOLVED

Fix 1:

Updating code-runner.executorMap property in settings.json as

"code-runner.executorMap": {
            "cpp": "cd $dirWithoutTrailingSlash && g++ -std=c++11 $fileName -o $fileNameWithoutExt && ./$fileNameWithoutExt"
        }

Fix 2:

Adding another property code-runner.terminalRoot in settings.json as:

"code-runner.terminalRoot": "/"

回答1:


I was having the same issue for quite some time and after hours of online loitering, I think I found the solution.

windows and unix/linux have different ways of naming the address. To address a folder named my_codes inside d drive, windows will have the following :-" D:\my_codes ", while unix / linux will have something like this-"/D/my_codes/". The difference worth noticing is use of different slashes( '/' & \ ). Unix considers \ as delimiter. So the address being fed to bash is not actually correct address for bash. And that is why it throws the error "No such file or directory".

Did you notice that just before bash threw you the error "No such file or directory", it removed all the back slashes from the address and presented you 'c:UsersTusharDesktopcontestPractice' ?

luckily this issue has already been resolved by "Code Runner" devs.

All you have to do is include the following in setting.json file:- "code-runner.terminalRoot":"/",

I hope I was of some help. Happy Coding.



来源:https://stackoverflow.com/questions/62721028/vscode-code-runner-extension-is-unable-to-execute-the-code-on-git-bash-terminal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!