代碼一鍵運行
安裝好Code Runner之后,打開你所要運行的文件,有多種方式來快捷地運行你的代碼:
- 鍵盤快捷鍵 Ctrl+Alt+N
- 快捷鍵 F1 調出 命令面板, 然后輸入 Run Code
- 在編輯區,右鍵選擇 Run Code
- 在左側的文件管理器,右鍵選擇 Run Code
- 右上角的運行小三角按鈕
停止代碼運行
如果要停止代碼運行,也有如下幾種方式:
- 鍵盤快捷鍵 Ctrl+Alt+M
- 快捷鍵 F1 調出 命令面板, 然后輸入 Stop Code Run
- 在Output Channel,右鍵選擇 Stop Code Run
Run in Terminal
在 GitHub Issue 中,用戶問到最多的問題就是亂碼和怎么支持輸入。通過設置,我們可以把代碼放到 VS Code 內置的 Terminal 來運行,這兩個問題就能迎刃而解了。
選擇 文件 -> 首選項 -> 設置,打開VS Code設置頁面,找到 Run Code configuration,勾上 Run In Terminal 選項。設置之后,代碼就會在 Terminal 中運行了。
自定義運行邏輯
對於一些語言,用戶希望能自定義代碼的運行邏輯。比如說,在 Code Runner 中,C++的默認編譯器用的是 g++,也許你希望使用 Clang。那么你可以在 VS Code 設置頁面,找到 Executor Map 設置項,並且選擇 在settings.json中編輯。
在 settings.json 中,添加 code-runner.executorMap 設置,然后就可以對不同的語言設置自定義的運行邏輯了。下面就是對 Java 配置的一個例子:
如果你想自定義代碼運行邏輯,你可以用到下面的一些變量,在運行時,Code Runner會把相應的變量進行替換:
- $workspaceRoot
- $dir
- $dirWithoutTrailingSlash
- $fullFileName
- $fileName
- $fileNameWithoutExt
code runner指定c/c++指定.exe文件生成路徑
"code-runner.executorMap": {
"c": " cd $dir && g++ $fileName -o ..\\out\\$fileNameWithoutExt.exe && ..\\out\\$fileNameWithoutExt.exe",
},
launch.json
{
// 使用 IntelliSense 了解相關屬性。
// 懸停以查看現有屬性的描述。
// 欲了解更多信息,請訪問: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "gcc.exe - 生成和調試活動文件",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}\\out\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "D:\\MinGW\\bin",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "D:\\MinGW\\bin\\gdb.exe",
"setupCommands": [
{
"description": "為 gdb 啟用整齊打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: gcc.exe 生成活動文件 ver(1)"
}
]
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc.exe 生成活動文件",
"command": "D:\\MinGW\\bin\\gcc.exe",
"args": [
"-g",
"${file}",
"-o",
"${workspaceFolder}\\out\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "D:\\MinGW\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "編譯器: D:\\MinGW\\bin\\gcc.exe"
},
{
"type": "cppbuild",
"label": "C/C++: gcc.exe 生成活動文件 ver(1)",
"command": "D:\\MinGW\\bin\\gcc.exe",
"args": [
"-g",
"${file}",
"-o",
"${workspaceFolder}\\out\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "D:\\MinGW\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "調試器生成的任務。"
}
]
}