代码之家  ›  专栏  ›  技术社区  ›  Dave Chambers

如何从Visual Studio代码任务传递参数。当命令包含空格时,将json转换为g++命令

  •  0
  • Dave Chambers  · 技术社区  · 7 年前

    我有一个 Visual Studio Code tasks.json 文件来运行 C++ 文件通过 g++ :

    {
    "version": "2.0.0",
    "tasks": [
        {
            "label": "echo",
            "type": "shell",
            "command": "g++",
            "args": [
                "-I ~/vcpkg/installed/x64-osx/include/",
                "test.cpp"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
    }
    

    问题在于生成的命令 > Executing task: g++ '-I ~/vcpkg/installed/x64-osx/include/' test.cpp < 有单引号,所以不起作用。

    我看了一眼 here 试过这个:

    {
    "version": "2.0.0",
    "tasks": [
        {
            "label": "echo",
            "type": "shell",
            "command": "g++",
            "args": [
                {
                  "value": "-I ~/vcpkg/installed/x64-osx/include/",
                  "quoting": "escape"
                },
                "test.cpp"
              ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
    }
    

    问题在于生成的命令 > Executing task: g++ -I\ ~/vcpkg/installed/x64-osx/include/ test.cpp < 使用 escape 还有一个 \ .

    如何生成所需的命令:

    g++ -I ~/vcpkg/installed/x64-osx/include/ test.cpp

    0 回复  |  直到 6 年前
        1
  •  0
  •   Steve    6 年前

    添加单引号是因为参数中有空格。只需将“-I”和“~/vcpkg/installed/x64 osx/include/”作为单独的参数。

    "args": [
        "-I", "~/vcpkg/installed/x64-osx/include/",
        "test.cpp"
    ]