尽管使用copts添加了额外的include目录,Bazel为什么仍找不到我的include文件?

时间:2018-10-25 20:50:07

标签: gcc 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吗?

2 个答案:

答案 0 :(得分:0)

cc_library中的cpp-tutorial/stage3/lib/BUILD没有声明对头文件的依赖,因此Bazel不会将该文件放入沙箱中,因此编译失败(应该如此)。

您不需要多余的copts。相反,您必须在{hello-time“规则的bar/baz.h中添加hdrs,或者,如果” bar“是一个单独的程序包(它具有BUILD文件),然后添加一个” cc_library ”,其中hdrs包括baz.h并依赖于“ hello-time”中的该库的规则。

答案 1 :(得分:0)

我在Bazel邮件列表中得到了这个答案(简短版本:此头文件需要添加到cc_library( name = "hello-time", srcs = [ "foo/bar/baz.h", "hello-time.cc", ], hdrs = ["hello-time.h"], copts = ["-Ilib/foo"], visibility = ["//main:__pkg__"], ) ,有点令人惊讶):

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

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

{{1}}