首页 > 解决方案 > VS Code Code Runner C++ 在终端中使用错误的命令

问题描述

我在 VS Code 中使用 Code Runner 扩展来运行 C++ 代码,并且它使用了错误的终端命令来执行此操作。我在 VS Code 和 Windows 10 中使用 git-bash。

这是终端中的命令:

Douglas@LAPTOP-6BSNLLDB MINGW64 /c/path (master)
$ cd "c:\path\" && g++ HelloWorld.cpp -o HelloWorld && "c:\path\"HelloWorld
bash: cd: c:\path" && g++ HelloWorld.cpp -o HelloWorld && c:path"HelloWorld: No such file or directory

如您所见,它正在使用cd并且g++正确,但是实际运行.exe文件的命令是错误的。不应该"c:\path\HellowWorld.exe"吗?

我怎样才能改变这个?它可以与 python 一起正常工作。

如果不能,如何在 VS Code 中运行 C++ 代码?

标签: c++visual-studio-codevscode-code-runner

解决方案


好像您使用的是 git-bash 而不是 PowerShell。

你只需要使用

"code-runner.terminalRoot": "/",

VSCode:Code Runner 扩展无法在 git bash 终端上执行代码?


将设置更改为

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

还有c的:(我用tcc)

"code-runner.executorMap":{
 "c": "cd $dirWithoutTrailingSlash && tcc -run $fileNameWithoutExt.c",
}

推荐阅读