簡體   English   中英

引用在 launch.json 中定義的 tasks.json 中的變量

[英]Reference a variabled in tasks.json that was defined in launch.json

我正在嘗試在 Visual Studio Code 中配置我的項目。
我正在使用 CMake+make 來構建我的項目。 CMake 在生成 make 文件之前需要了解項目配置(調試/發布)和體系結構(x64/x86)。

我可以定義 4 個任務和 4 個啟動配置來實現這一點(每個都硬編碼兩個選項),但這是很多重復。
相反,我想使用環境變量基本上將參數從啟動配置“傳遞”到單個任務。

但是,VScode 似乎沒有將我在啟動配置中定義的內容轉發給任務。

這是我所擁有的:

// launch.json
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Debug (x64)",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/bin/debug/x64/executable",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [
                {
                    "name": "proj_configuration",
                    "value": "Debug"
                },
                {
                    "name": "proj_platform",
                    "value": "x64"
                }
            ],
            // "variables": {
            //     "proj_configuration": "Debug",
            //     "proj_platform": "x64"
            // },
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "/usr/bin/gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "Make Project",
        }
    ]
}

// tasks.json
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Build Project",
            "type": "shell",
            "command": "cmake",
            "args": [
                "-B${workspaceFolder}/build",
                "-H${workspaceFolder}",
                "-DCMAKE_BUILD_TYPE=${proj_configuration}",
                "-DCMAKE_GENERATOR_PLATFORM=${proj_platform}"
            ],
            "group": {
                "kind": "build",
                "isDefault": false
            }
        },
        {
            "label": "Make Project",
            "type": "shell",
            "command": "make",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "dependsOn": ["Build Project"],
        }
    ]
}

當我嘗試運行我的項目時,我得到

執行任務:cmake -B/home/user/project/build -H/home/user/project/ -DCMAKE_BUILD_TYPE=${proj_configuration} -DCMAKE_GENERATOR_PLATFORM=${proj_platform}

如您所見,這 2 個變量沒有被替換。
我也試過使用屬性“變量”,但也沒有成功。

那么,是否有可能按照我描述的那樣做,還是我必須定義多個任務呢?

使用擴展命令變量,您可以在存儲中保存多個字符串,並在 launch.json 和 task.json 中使用它們。 請參閱鏈接中的示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM