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

$ 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

然后,您可以使用以下命令构建应用程序:
bazel build //:app
并在copts -flag中指定包含路径:
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" ],
)

关于c++ - 如何使用Bazel构建这个简单的示例?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55101352/

10-17 00:00