如何在外部终端(命令提示符)中制作 Code Runner 运行?

How to make Code Runner run in external terminal (Command Prompt)?

所以,基本上 Visual Studio 中的 Code Runner 代码可以 运行 在集成终端中。我怎样才能在外部终端中达到运行,这是命令提示符,因为我需要向同学展示我的程序输出,所以通过集成的显示它不方便。

我知道有像Dev-C++这样的软件可以在外部终端运行,但我喜欢用这个VS Code,因为它干净UI,而且Code Runner插件很漂亮做好自己的工作。如何一键完成?有配置吗?

假设您使用的是Windows,您只需要在代码中运行您要运行的.exe文件前面加上"start"命令即可-runner.executorMap 选项。这是一个例子:

"code-runner.executorMap": {
        "cpp": "g++ $fullFileName -o $fileNameWithoutExt.exe && start $fileNameWithoutExt.exe"
}

对于Linux:

If you are running linux then you can paste this in code-runner.executorMap

如果您想使用 gnome 终端执行代码:

"code-runner.executorMap": {
        "c": "cd $dir && gcc $fileName -o $fileNameWithoutExt && gnome-terminal -- bash -c './$fileNameWithoutExt ; read line'",
        "cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt && gnome-terminal -- bash -c './$fileNameWithoutExt ; read line'",
}
        

如果您是 运行 ubuntu 或者如果您有 gnome 终端,请使用它。 'read line'用于保持window打开,直到代码执行后按下任意键

如果您想使用 Xterm 执行您的代码:

    "code-runner.executorMap": {
        "c": "cd $dir && gcc $fileName -o $fileNameWithoutExt && xterm -hold ./$fileNameWithoutExt",
        "cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt && xterm -hold ./$fileNameWithoutExt"
    }

如果你有 xterm,你可以使用它。 -hold 标志用于在代码执行后保持 window 打开

On Windows,当 Default Shell 在 VSCode 中设置为 PowerShell 7 (pwsh.exe) 我使用:

"code-runner.executorMap": {
    "c": "cd $dir && gcc -Wall -Wextra -pedantic -std=c18 $fileName -o $fileNameWithoutExt.exe && Start-Process -FilePath pwsh -ArgumentList \"-Command  &{.\$fileNameWithoutExt.exe; pause}\"",
}

当默认值 Shell 在 VSCode 中设置为命令提示符 (cmd.exe) 我使用

"code-runner.executorMap": {
     "c": "cd $dir && gcc -Wall -Wextra -pedantic -std=c18 $fileName -o $fileNameWithoutExt.exe && Start pwsh -Command  \"& {.\$fileNameWithoutExt.exe; pause}\"",
}

在这两种情况下,“暂停”将在代码执行后保持外部电源Shell 7 终端打开。

如果有人想知道如何编辑 "code-runner.executorMap"":{} 以上答案都提供了进入 settings.json 的方法。所以我写这个答案只是为了阐明如何进入代码运行程序设置。

转到 文件 -> 首选项 -> 设置 并搜索“executorMap' in search tab -> 然后点击 Code-runner:Executor Map 然后编辑代码如下 C,

"code-runner.executorMap": {
        "c": "g++ $fullFileName -o $fileNameWithoutExt.exe && start $fileNameWithoutExt.exe"
}

对于C++编辑代码如下,

"code-runner.executorMap": {
        "cpp": "g++ $fullFileName -o $fileNameWithoutExt.exe && start $fileNameWithoutExt.exe"
}

Note: This solution is for windows environments

如果您使用的是 Code Runner,您可以将其添加到您的 settings.json:

    "code-runner.executorMap":{
        "cpp": "cd $dir && g++ -O2 -std=c++17 $fileName -o $fileNameWithoutExt && start cmd \"/k ; $fileNameWithoutExt\""
    },

这将在程序执行后保留cmd

我不知道为什么 konsole -- bash -c "command" 命令现在适用于 kde konsole 所以,如果你使用 kde 然后安装 gnome-terminal第一 如果你想 运行 一个外部终端然后添加这个到 settings.json vscode

"code-runner.runInTerminal": true,
"code-runner.preserveFocus": false,
"code-runner.executorMap": {
    "c": "cd $dir && echo $PWD > /tmp/vsDir && echo $fileName > /tmp/vsFile && gnome-terminal -- bash -c 'dir=\"$(cat /tmp/vsDir)\" && cd $dir;File=\"$(cat /tmp/vsFile)\" && gcc $File -lm;./a.out;echo Hit Enter to EXIT;read lines;rm *.out' && exit",
    "cpp": "cd $dir && echo $PWD > /tmp/vsDir && echo $fileName > /tmp/vsFile && gnome-terminal -- bash -c 'dir=\"$(cat /tmp/vsDir)\" && cd $dir;File=\"$(cat /tmp/vsFile)\" && g++ $File -lm;./a.out;echo Hit Enter to EXIT;read lines;rm *.out' && exit",
    "java": "cd $dir && echo $PWD > /tmp/vsDir && echo $fileName > /tmp/vsFile && echo $fileNameWithoutExt > /tmp/javaRun && gnome-terminal -- bash -c 'dir=\"$(cat /tmp/vsDir)\" && cd $dir;File=\"$(cat /tmp/vsFile)\" && javac $File;binary=\"$(cat /tmp/javaRun)\";java $binary;echo Hit Enter to EXIT;read lines;rm *.class' && exit",
    "python": "cd $dir && echo $PWD > /tmp/vsDir && echo $fileName > /tmp/vsFile && gnome-terminal -- bash -c 'cd;source /home/$USER/anaconda3/bin/activate; conda activate MyPy38;dir=\"$(cat /tmp/vsDir)\" && cd $dir;File=\"$(cat /tmp/vsFile)\" && python -u $File;echo Hit Enter to EXIT;read lines' && exit",
  },
  "terminal.integrated.inheritEnv": false

如果您不想删除 binary or exe or class 文件,请从中删除 rm *.outrm *.out 或类似命令。

对于 Anaconda Python source /home/$USER/anaconda3/bin/activate; 用于激活基础 anaconda 环境,然后在 conda activate MyPy38; 激活我的环境名称 MyPy38。如果您从默认 python3pip 库中 运行 python 然后将命令替换为

"python": "cd $dir && echo $PWD > /tmp/vsDir && echo $fileName > /tmp/vsFile && gnome-terminal -- bash -c 'cd;dir=\"$(cat /tmp/vsDir)\" && cd $dir;File=\"$(cat /tmp/vsFile)\" && python3 -u $File;echo Hit Enter to EXIT;read lines' && exit",

或者,您可以使用项目基础 python envs “假设 python 解释器目录是 /From/Base/To/Python/Binary/ 目录”,那么它将是

"python": "cd $dir && echo $PWD > /tmp/vsDir && echo $fileName > /tmp/vsFile && gnome-terminal -- bash -c 'cd;dir=\"$(cat /tmp/vsDir)\" && cd $dir;File=\"$(cat /tmp/vsFile)\" && /From/Base/To/Python/Binary//python -u $File;echo Hit Enter to EXIT;read lines' && exit",