I've posted this question to Stackoverflow (https://stackoverflow.com/questions/52997911/why-doesnt-bazel-find-my-include-file-despite-using-copts-to-add-an-extra-inclu
), but since I got not response so far, I'm trying my luck here.
I've modified the stage3 cpp bazelbuild example to use an extra include paths via copts. My goal is to have a directory with "private" include files that are used by lib but not visible to main.
https://github.com/mnieber/examples/commit/a8b784ddf5698563a31401b9ac3531636b3536ef
However, this produces a compiler error (note though that -Ilib/foo is used as an option for 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
**********************************************************************
Can someone explain why bar/baz.h is not found? Is it because I did not tell Bazel about the existence of the lib/foo/bar/baz.h file? If so, then how can I tell Bazel without making lib/foo/bar/baz.h visible to main?
cc_library(
name = "hello-time",
srcs = [
"foo/bar/baz.h",
"hello-time.cc",
],
hdrs = ["hello-time.h"],
copts = ["-Ilib/foo"],
visibility = ["//main:__pkg__"],
)