首页 > 解决方案 > 尽管使用 copts 添加了额外的包含目录,为什么 Bazel 找不到我的包含文件?

问题描述

我已经修改了 stage3 cpp bazelbuild 示例以使用额外的包含路径copts

https://github.com/mnieber/examples/commit/a8b784ddf5698563a31401b9ac3531636b3536ef

但是,这会产生编译器错误(请注意,尽管它-Ilib/foo用作 的选项gcc):

bazel build --verbose_failures //main:hello-world
INFO: Analysed target //main:hello-world (1 packages loaded).
INFO: Found 1 target...
ERROR: /home/maarten/sources/examples/cpp-tutorial/stage3/lib/BUILD:1:1: C++ compilation of rule '//lib:hello-time' failed (Exit 1): gcc failed: error executing command 
  (cd /home/maarten/.cache/bazel/_bazel_maarten/62d72ea3bd73864cf884808e7d850715/execroot/__main__ && \
  exec env - \
    LD_LIBRARY_PATH=/usr/local/lib \
    PATH=/home/maarten/projects/xmlparser/dodo_commands/env/bin:/home/maarten/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/dodo/env/bin:/home/maarten/.dodo_commands/bin \
    PWD=/proc/self/cwd \
  /usr/bin/gcc -U_FORTIFY_SOURCE -fstack-protector -Wall -B/usr/bin -B/usr/bin -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer '-std=c++0x' -MD -MF bazel-out/k8-fastbuild/bin/lib/_objs/hello-time/hello-time.pic.d '-frandom-seed=bazel-out/k8-fastbuild/bin/lib/_objs/hello-time/hello-time.pic.o' -fPIC -iquote . -iquote bazel-out/k8-fastbuild/genfiles -iquote bazel-out/k8-fastbuild/bin -iquote external/bazel_tools -iquote bazel-out/k8-fastbuild/genfiles/external/bazel_tools -iquote bazel-out/k8-fastbuild/bin/external/bazel_tools -Ilib/foo -fno-canonical-system-headers -Wno-builtin-macro-redefined '-D__DATE__="redacted"' '-D__TIMESTAMP__="redacted"' '-D__TIME__="redacted"' -c lib/hello-time.cc -o bazel-out/k8-fastbuild/bin/lib/_objs/hello-time/hello-time.pic.o)

Use --sandbox_debug to see verbose messages from the sandbox
lib/hello-time.cc:2:21: fatal error: bar/baz.h: No such file or directory
compilation terminated.
Target //main:hello-world failed to build

有人可以解释为什么bar/baz.h找不到吗?

标签: gccbazel

解决方案


我在 Bazel 邮件列表上得到了这个答案(简短版:这个头文件需要添加到srcs,有点令人惊讶):

问题是您没有在 cc_library 的源代码中以任何方式声明头文件 baz.h。因此,当在沙箱中执行时(默认),文件是不可见的。该构建已经在没有沙箱的情况下工作(尝试使用 --spawn_strategy=standalone 标志运行它)。

因此,在源代码中声明 baz.h:

cc_library(
    name = "hello-time",
    srcs = [
        "foo/bar/baz.h",
        "hello-time.cc",
    ],
    hdrs = ["hello-time.h"],
    copts = ["-Ilib/foo"],
    visibility = ["//main:__pkg__"],
)

推荐阅读