尽管使用 copts 添加额外的包含目录,为什么 Bazel 找不到我的包含文件?
Why doesn't Bazel find my include file despite using copts to add an extra include directory?
我修改了 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
吗?
cpp-tutorial/stage3/lib/BUILD
中的 cc_library
没有声明对头文件的依赖,所以 Bazel 没有将该文件放入沙箱,因此编译失败(应该如此)。
您不需要额外的 copts
。相反,您必须将 bar/baz.h
添加到 "hello-time" 规则的 hdrs
中,或者,如果 "bar" 是一个单独的包(它有一个 BUILD 文件),则添加一个"cc_library" 规则 hdrs
包括 baz.h
并依赖于来自 "hello-time".
的库
我在 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__"],
)
我修改了 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
吗?
cpp-tutorial/stage3/lib/BUILD
中的 cc_library
没有声明对头文件的依赖,所以 Bazel 没有将该文件放入沙箱,因此编译失败(应该如此)。
您不需要额外的 copts
。相反,您必须将 bar/baz.h
添加到 "hello-time" 规则的 hdrs
中,或者,如果 "bar" 是一个单独的包(它有一个 BUILD 文件),则添加一个"cc_library" 规则 hdrs
包括 baz.h
并依赖于来自 "hello-time".
我在 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__"],
)