1. 程式人生 > 其它 >VScode cdoe runner--指定.exe檔案生成路徑

VScode cdoe runner--指定.exe檔案生成路徑

程式碼一鍵執行

安裝好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": "偵錯程式生成的任務。"
        }
    ]
}