【问题标题】:Generated header not found未找到生成的标头
【发布时间】:2018-06-25 13:37:36
【问题描述】:

我正在尝试使用 Bazel 构建一个使用 Flatbuffers 的 cpp 项目。 但是我用flatc生成的map_schema_generated.h没有找到。

我的树:

|
|_ data
|  |_ maps
|     |_ BUILD
|     |_ map_schema.fbs
|
|_ src
|  |_ map
|     |_ BUILD
|     |_ map.hpp
|     |_ map.cpp
|
|_ tools
|  |_ BUILD
|  |_ generate_fbs.bzl
|
|_ WORKSPACE

tools/generate_fbs.bzl:

def _impl(ctx):
  output = ctx.outputs.out
  input = ctx.files.srcs
  print("generating", output.basename)
  ctx.action(
      use_default_shell_env = True,
      outputs = [output],
      inputs = input,
      progress_message="Generating %s with %s" % (output.path, input[0].path),
      command="flatc -o %s --cpp %s" % (output.dirname, input[0].path)
  )

generate_fbs = rule(
  implementation=_impl,
  output_to_genfiles = True,
  attrs={
       "srcs": attr.label_list(allow_files=True, allow_empty=False),
       "out": attr.output()
       },
)

data/maps/BUILD:

load("//tools:generate_fbs.bzl", "generate_fbs")

generate_fbs(
  name = "schema",
  srcs = ["map_schema.fbs"],
  out = "map_schema_generated.h",
  visibility = ["//visibility:public"]
)

src/map/BUILD:

cc_library(
  name = "map",
  srcs = [
    "//data/maps:map_schema_generated.h",
    "map.hpp",
    "map.cpp"
  ]
)

src/map/map.cc 拥有#include "map_schema_generated.h"

我用来构建的命令行:bazel build //src/map

如果我在bazel-*find,我得到:

bazel-genfiles/data/maps/map_schema_generated.h
bazel-out/k8-fastbuild/genfiles/data/maps/map_schema_generated.h
bazel-my-workspace-name/bazel-out/k8-fastbuild/genfiles/data/maps/map_schema_generated.h

如果我cat这些文件,我可以看到它们生成良好。

我找到的所有信息都是关于Tensorflow,并没有真正的帮助。

最好的问候,

【问题讨论】:

  • 对 Bazel 不是很熟悉,但是考虑到它会构建和生成文件,问题一定是你的 cc_library 将标头定义为依赖项,但没有将其指定为包含 /包括目录。我会看看是否有办法查看生成的编译器命令行,如果那里没有生成文件目录的-I,那么这就是你的问题。
  • Ok bazel build -s //src/map 打印编译器选项。 -I 不见了。所以我将copts = ["-I$(GENDIR)/data/maps"] 添加到src/map/BUILD 中,它可以工作。谢谢你的建议。 (但我认为这不是好方法)

标签: c++ bazel flatbuffers


【解决方案1】:

我可以写 `#include "data/maps/map_schema_g​​enerated.h",而不是 src/map/map.cpp 中的 #include "map_schema_generated.h"

我认为这是让它工作的最干净的方法。

【讨论】:

    【解决方案2】:

    问题是您的cc_library 实际上并没有真正识别您生成的标头需要任何特殊操作(例如为其所在位置添加-I 标志)。它被生成并存在于构建树中,但编译器(预处理器)不会在任何地方寻找它在map.cpp 上工作。 (使用-s 运行构建,以更深入地了解发生了什么以及如何发生)。

    现在关于如何解决这个问题,可能有更好的方法,但这似乎可行。我想这个功能也可以融入generate_fbs 规则。

    data/maps/BUILD 我添加了“仅标题”库,如下所示:

    cc_library(
      name = "map_schema_hdr",
      hdrs = [":map_schema_generated.h"],
      include_prefix = ".",  # to manipulate -I of dependenices
      visibility = ["//visibility:public"]
    )
    

    src/map/BUILD 中,我将使用这个仅标头库作为map 的依赖项:

    cc_library(
      name = "map",
      srcs = [
        "map.cpp"
        "map.hpp"
      ],
      deps = [
         "//data/maps:map_schema_hdr",
      ]
    )
    

    为了方便起见,为了更多地考虑使用单一规则(宏)的想法,我进行了以下更改:

    tools/generate_fbs.bzl 现在改为:

    def _impl(ctx):
      output = ctx.outputs.out
      input = ctx.files.srcs
      print("generating", output.basename)
      ctx.action(
          use_default_shell_env = True,
          outputs = [output],
          inputs = input,
          progress_message="Generating %s with %s" % (output.path, input[0].path),
          command="/bin/cp %s %s" % (input[0].path, output.path)
      )
    
    _generate_fbs = rule(
      implementation=_impl,
      output_to_genfiles = True,
      attrs={
           "srcs": attr.label_list(allow_files=True, allow_empty=False),
           "out": attr.output()
           },
    )
    
    
    def generate_fbs(name, srcs, out):
      _generate_fbs(
        name = "_%s" % name,
         srcs = srcs,
         out = out
      )
    
      native.cc_library(
        name = name,
        hdrs = [out],
        include_prefix = ".",
        visibility = ["//visibility:public"],
      )
    

    有了它,我可以拥有data/maps/BUILD

    load("//tools:generate_fbs.bzl", "generate_fbs")
    
    generate_fbs(
      name = "schema",
      srcs = ["map_schema.fbs"],
      out = "map_schema_generated.h",
    )
    

    src/map/BUILD 包含:

    cc_library(
      name = "map",
      srcs = [
        "map.cpp",
        "map.hpp",
      ],
      deps = [
         "//data/maps:schema",
      ]
    )
    

    bazel build //src/map 构建bazel-bin/src/map/libmap.abazel-bin/src/map/libmap.so

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-28
      • 2016-01-26
      • 2013-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-01
      • 1970-01-01
      相关资源
      最近更新 更多