【问题标题】:What is the best method for compiling a project with multiple directories in vscode?在 vscode 中编译具有多个目录的项目的最佳方法是什么?
【发布时间】:2021-09-27 23:39:48
【问题描述】:

问题很简单。假设您在一个项目中有 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。

【问题讨论】:

  • 使用构建工具,Make,CMake,......

标签: c++ visual-studio-code g++


【解决方案1】:

您可以创建一个 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 并选择Tasks: Run Build Task
  • 点击终端 > 运行构建任务...

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

【讨论】:

  • 谢谢!我看到了文档,但由于它以 TypeScript 为特色,我认为它与我的情况无关。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多