首页 > 解决方案 > 使用 Visual Studio Code 任务自动化多个文件夹中的 C makefile

问题描述

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

我在项目主文件夹内的 .vscode 目录中创建了 tasks.json 文件。tasks.json 当前包含我基于本教程的以下代码。

{
    "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/project,然后make工作?我需要用跑步者把它分成几个不同的任务吗?我是使用 VS Code 的新手,因此不胜感激。谢谢。

标签: cjsonmakefilebuildvisual-studio-code

解决方案


我实现的解决方案如下:

{
    "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 的建议以及Microsoft 页面上的这篇文章。这里要注意的要点是,我没有向 shell 传递任何命令,而只是传递了 args。

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


推荐阅读