如何解决无聊的“未声明的包含"?错误? [英] How to resolve bazel "undeclared inclusion(s)" error?

查看:125
本文介绍了如何解决无聊的“未声明的包含"?错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是bazel的新手,并且无法使用来构建我的C ++包

I'm new to bazel, and I'm getting a failure to build my C++ package with

错误:/path/to/package/BUILD:linenumber:1未声明包含在规则'//path/to/package:name'中: 该规则缺少'path/to/package/source_file.cpp'所包含的以下文件的依赖项声明

ERROR: /path/to/package/BUILD:linenumber:1 undeclared inclusion(s) in rule '//path/to/package:name': this rule is missing dependency declarations for the following files included by 'path/to/package/source_file.cpp'

...之后是不同目录中的头文件列表.这些文件不是正在生成的程序包的一部分,而是从其他位置拉入的.

...followed by a list of header files in a different directory. These files are not part of the package that is being built, but are being pulled in from elsewhere.

我的问题是如何将声明正确添加到BUILD文件中以解决错误?

根据在线Bazel文档[a href ="https://bazel.build/versions/master/docs/be/c-cpp.html#cc_library" rel ="noreferrer">此处应该将每个标头添加到 srcs 列表中. (要清楚,这些是我正在构建的库内部使用的标头,并且是公共接口的 不是 部分,因此它们不属于 hdrs .)但是,如果我尝试这样做,

According to the online Bazel docu here I should add each header to the srcs list. (To be clear, these are headers that are used internally by the library I'm building and not part of the public interface, so they don't belong in hdrs.) But If I try that,

  srcs = [ ..., "path/to/dependent/headers/header.h",]

我收到一条错误消息

错误:...越过子包的边界...(也许您打算 把冒号放在这里:...?)

ERROR: ... crosses boundary of subpackage ... (perhaps you meant to put the colon here: ...?)

因为带有标头的目录不是Bazel软件包.

because the directory with the headers isn't a Bazel package.

如果我尝试按照该错误消息的提示将final/更改为冒号,

If I try changing the final / to a colon as that error message suggests,

  srcs = [ ..., "path/to/dependent/headers:header.h",]

然后

错误:...目标名称可能不包含':'.

ERROR: ... target names may not contain ':'.

此处中的Bazel C ++教程路径"表示外部包含目录应通过 copts 声明:

The Bazel C++ tutorial here, in the section "Additonal Include Paths" says that external include directories should be declared via copts:

cc_library(
    name = "some_lib",
    srcs = ["some_lib.cc"],
    hdrs = ["some_lib.h"],
    copts = ["-Ithird_party/some_lib"],
)

但是添加-I标志确实 不是 摆脱了未声明的包含物"错误!

But adding that -I flag does not git rid of the "undeclared inclusion(s)" error!

$ bazel version
Build label: 0.4.3
Build target: bazel-out/local-fastbuild/bin/src/main/java/com/google/devtools/build/lib/bazel/BazelServer_deploy.jar
Build time: Thu Dec 22 12:31:25 2016 (1482409885)
Build timestamp: 1482409885
Build timestamp as int: 1482409885

推荐答案

Bazel希望您依赖标头(即,将它们放在deps中).基本上,您应该为这些标头创建一个cc_library.将标头放入hdrs不会公开公开它们,而只是将其公开给依赖于该库的规则(这正是您想要的).因此,您将拥有:

Bazel wants you to depend on the headers (i.e., put them in deps). Basically, you should create a cc_library for those headers. Putting headers in hdrs doesn't publicly expose them, it just exposes them to rules that depend on that library (which is exactly what you want). So you'll have:

# third_party/some_lib/BUILD
cc_library(
    name = "headers",
    hdrs = glob(["*.h"]),
    visibility = ["//path/to/package:__pkg__"],
)

请注意,您应该将//path/to/package替换为实际目标的包,但是上面的__pkg__是文字的:这就是您表示对该包可见"的方式.这样,其他任何程序包都无法访问这些标头.

Note that you should replace //path/to/package with your actual target's package, but the __pkg__ above is literal: that's how you indicate "visible to that package". Then no other packages can access those headers.

然后在目标的deps中添加//third_party/some_lib:headers.

Then add //third_party/some_lib:headers in your target's deps.

copts仅用于修改C ++的标头搜索路径,而不是Bazel的. Bazel始终假设您会执行#include "path/relative/to/your/workspace/dir.h",但是如果您有类似这样的消息来源:

The copts are just used to modify C++'s header search paths, not Bazel's. Bazel always assumes that you'll do #include "path/relative/to/your/workspace/dir.h", but if you've got a source like:

#include "foo.h"

其中foo.h位于third_party/some_lib/includes/foo.h,您可以说copts = ["-Ithird_party/some_lib/includes"]将该名称添加到C ++的标头搜索路径中.

where foo.h is at third_party/some_lib/includes/foo.h, you could say copts = ["-Ithird_party/some_lib/includes"] to add that to C++'s header search path.

这篇关于如何解决无聊的“未声明的包含"?错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆