无法通过 bazel 的规则成功包含外部头文件cc_library

Cannot successfully include external header file through cc_library rule for bazel

本文关键字:文件 cc library 包含外 成功 bazel 规则      更新时间:2023-10-16

我无法通过 bazel 中的构建命令包含一些头文件。我遵循了它们包含在 bazel 文档中的示例。

这是我的构建文件

cc_library(
name = "hello-greet",
srcs = ["hello-greet.cc"],
hdrs = ["hello-greet.h"],
copts = ["-Imain/include"]
)
cc_binary(
name = "hello-world",
srcs = ["hello-world.cc"],
deps = [
":hello-greet",
],
)

下面是我的目录结构。

  • 工作
  • 主要
    • 包括
      • 你好问候。
    • hello-greet.cc
    • hello-world.cc

我不知道这是否有帮助,但这里有一些源文件和头文件的代码。

hello-greet.cc

#include "hello-greet.h"
#include <string>
std::string get_greet(const std::string& who) {
return "Hello " + who;
}

hello-world.cc

#include "hello-greet.h"
#include <ctime>
#include <iostream>
#include <string>
void print_localtime() {
std::time_t result = std::time(nullptr);
std::cout << std::asctime(std::localtime(&result));
}
int main(int argc, char** argv) {
std::string who = "world";
if (argc > 1) {
who = argv[1];
}
print_localtime();
return 0;
}

当我运行 bazel 构建命令时,它抱怨此错误

INFO: Found 1 target...
ERROR: missing input file '//main:hello-greet.h'.

您应该能够通过向cc_library添加includes = "."来做您想做的事(不需要有copts(