在 vscode 中编译具有多个目录的项目的最佳方法是什么?

What is the best method for compiling a project with multiple directories in vscode?

问题很简单。假设你在一个项目中有 3 个文件,我想将它们全部编译。

src/proj-runner/main.cpp

#include <iostream>
using namespace std;

int main () {
  string line;
  Greeting *hello = new Greeting();
  cout << hello->hello() << endl;

  return 0;
}

src/proj-class/class.cpp

#include "class.h"

Greeting::Greeting() {
    
}

string Greeting::hello() {
    return "Hello World!";
}

src/proj-class/class.h

#include <iostream>
using namespace std;

class Greeting {
    public:
        Greeting();
        string hello();
};

如何不用每次都输入 g++ proj-runner/main.cpp proj-class/class.cpp?我在 linux.

上使用 VSCode

您可以创建一个 vscode 构建任务到 运行 您的命令。为此,请在您的工作区根目录中创建一个 .vscode 目录,并在该目录中创建一个名为 tasks.json 的文件。将以下内容添加到该文件:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build_main_and_class",
            "type": "shell",
            "command": "g++ proj-runner/main.cpp proj-class/class.cpp",
            "problemMatcher": [],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

那么您只需执行以下任一操作即可:

  • Command+Shift+B
  • Command+Shift+P 和 select Tasks: Run Build Task
  • 单击终端 > 运行 生成任务...

您可以在此处的 vscode 文档中阅读有关任务的更多信息:https://code.visualstudio.com/docs/editor/tasks#_typescript-hello-world