我如何用巴泽尔建立这个简单的例子

How do I build this simple example with Bazel?

本文关键字:简单 建立 何用巴      更新时间:2023-10-16

假设我有一个这样的项目:

$ tree . 
├── WORKSPACE
├── include
│   └── header.hpp
└── main.cpp
└── BUILD.bazel

main.cpp看起来像这样:

#include "header.hpp"
int main() {
  return 0;
}

我的BUILD.bazel文件应该是什么样的?

我目前的尝试:

cc_binary(
  name = "app",
  srcs = [
    "main.cpp",
    "include/header.hpp",
  ],
)

编辑:忘了提及我的WORKSPACE文件


编辑:找到了一个工作,但我认为它不是很优雅:

cc_library(
  name = "app-hdrs",
  hdrs = [
    "include/header.hpp",
  ],
  srcs = [
    "include/header.hpp",
  ],
  strip_include_prefix = "include",
)
cc_binary(
  name = "app",
  srcs = [
    "main.cpp",
  ],
  deps = [
    ":app-hdrs",
  ],
)

您需要一个在项目文件夹中称为 WORKSPACE的文件:

$ tree . 
├── include
│   └── header.hpp
└── main.cpp
└── BUILD.bazel
└── WORKSPACE

然后,您可以使用以下commmand构建应用程序:

bazel build //:app

,还指定了copts -Flag中的Incluble路径:

cc_binary(
  name = "app",
  srcs = [
    "main.cpp",
    "include/header.hpp",
  ],
  copts = ["-Iinclude", "-Wall", "-Werror"],
)

cc_binary(
  name = "app",
  includes = [ "include" ],
  srcs = [
    "main.cpp",
    "include/header.hpp",
  ],
  copts = [ "-Wall", "-Werror" ],
)