【问题标题】:Using Visual Studio Code tasks to automate C makefiles in multiple folders使用 Visual Studio Code 任务自动化多个文件夹中的 C makefile
【发布时间】:2019-09-05 15:10:09
【问题描述】:

我有一个用 C 语言编写的项目,它有两个 make 文件,一个在主目录中,一个在子目录中。我想使用 VS Code 任务来自动化制作过程,这样我每次想运行时都不必使用终端来制作项目。相反,我只想使用ctrl+shift+b,然后使用mpiexec 从终端运行我的代码。

我在项目主文件夹中的 .vscode 目录中创建了 tasks.json 文件。 tasks.json 目前包含以下代码,我基于 this tutorial.

{
    "version": "2.0.0",
    "command": "bash",
    "tasks": [
        {
            "label": "Make Project",
            "type": "shell",
            "command": "cd ${workspaceFolder}",
            "args": ["make"],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": false,
                "panel": "shared",
                "showReuseMessage": true,
                "clear": false
            },
            "problemMatcher": "$msCompile"
        }
    ]
}

我希望这与在当前工作目录的位置在终端中键入make 的作用相同。相反,这是终端的输出

Executing task: 'cd /home/git/project' make <
/bin/bash: cd /home/git/project: no such file or directory
The terminal process terminated with exit code: 127

Terminal will be reused by tasks, press any key to close it.

请注意,make 文件位于 /home/git/project。然后我希望在 /home/git/project/subfolder 中构建一个 make 文件。

为什么初始命令cd /home/git/projectmake 不起作用?我需要用跑步者把它分成几个不同的任务吗?我是使用 VS Code 的新手,因此不胜感激。谢谢。

【问题讨论】:

    标签: c json makefile build visual-studio-code


    【解决方案1】:

    我实现的解决方案如下:

    {
        "version": "2.0.0",
        "tasks": [
            {
                "label": "msvc build",
                "type": "shell",
                "command": "",
                "args": [
                    "make",
                    "--directory=${workspaceFolder};",
                    "make", 
                    "--directory=${workspaceFolder}/subfolder"
                ],
                "group":  {
                    "kind": "build",
                    "isDefault": true
                },
                "presentation": {
                    "reveal":"always"
                },
                "problemMatcher": "$msCompile"
            }
        ]
    }
    

    基于Reinier Torenbeek's suggestionthis post on Microsoft's page. 这里要注意的要点是我没有向shell 传递任何命令,而只是传递了args。

    我现在可以通过在编辑器中按ctrl+shift+b 来构建项目中的所有makefile,因为group 设置为build

    【讨论】:

      【解决方案2】:

      为什么初始命令 cd /home/git/project 然后 make 不起作用?

      输出

      Executing task: 'cd /home/git/project' make
      

      表明整个表达式 cd /home/git/project 被解释为单个命令名称(包括空格,它是文件名中的有效字符),make 作为其参数。当您查看 json 配置的结构时,这是有道理的。由于不存在这样的命令,所以它失败了。

      可能有几种方法可以让它工作,--directorymake 选项可能是一个好方法。见9.7 Summary of Options

      【讨论】:

      • 谢谢,使用--directory 是解决此问题的正确途径。我将在下面添加我的解决方案的代码。
      猜你喜欢
      • 1970-01-01
      • 2019-01-07
      • 2015-10-20
      • 1970-01-01
      • 2023-03-10
      • 2020-01-21
      • 2016-12-26
      • 1970-01-01
      • 2018-03-07
      相关资源
      最近更新 更多