C++: How to add an include path without relying on 'deps'?

4,030 views
Skip to first unread message

brooks...@gmail.com

unread,
Oct 9, 2015, 7:11:13 PM10/9/15
to bazel-discuss
Given the following project:

[workspace]/
WORKSPACE
include/
build.h
a/
BUILD
a.h
a.cc
b/
BUILD
b.h
b.cc
main.cc

When I add "#include "include/build.h"" to any .h or .cc source file, I get an error saying build.h cannot be found.

I thought includes were relative to the workspace and figured this would work.

I'd like to avoid adding a include/BUILD with a cc_library() for just these header files, and then adding it to the 'deps' of every other target.

Is there a simpler way?

Austin Schuh

unread,
Oct 9, 2015, 7:40:22 PM10/9/15
to brooks...@gmail.com, bazel-discuss
It is actually a feature of Bazel that it won't let you depend on build.h without declaring it as a dependency.  You may be able to add a default set of dependencies in //tools/cpp/BUILD, but that isn't "simple".

Austin

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/99b30903-82b9-4bca-84f2-12afff2f0fef%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

David Chen

unread,
Oct 9, 2015, 10:31:03 PM10/9/15
to Austin Schuh, bazel-...@googlegroups.com, brooks...@gmail.com

What Austin said is correct. You cannot directly include include/build.h because build.h is in a different package (//include). You would need to add a include/BUILD and add a cc_library target that //a and //b can depend on in order to use build.h.

David

Garrett Garcia

unread,
Feb 9, 2016, 8:37:56 PM2/9/16
to bazel-discuss, austin...@gmail.com, brooks...@gmail.com
Can I create a cc_library target that only includes the necessary header file?  And if so, how?  Otherwise if I added a dependency for an existing cc_library that exposed include/build.h, this would create a false dependency on that library for the sole purpose of using that single header file.

Brian Silverman

unread,
Feb 9, 2016, 8:43:05 PM2/9/16
to Garrett Garcia, bazel-discuss, Austin Schuh, brooks...@gmail.com
Yes, you can just make a cc_library which has only hdrs and no srcs. Bazel won't actually create any output files for it, but it will propagate the information about the header files.

Garrett Garcia

unread,
Feb 9, 2016, 10:23:05 PM2/9/16
to Brian Silverman, bazel-discuss, Austin Schuh, brooks...@gmail.com
Thanks, Brian.  I can't seem to get that to work.  I'm getting the following error:

ERROR: /BUILD:10:12: in srcs attribute of cc_library rule //oc/common:upnputil: '//cc:sonos_stdint' does not produce any cc_library srcs files (expected .cc, .cpp, .cxx, .c++, .C, .c, .h, .hh, .hpp, .hxx, .inc, .S, .s, .asm, .a, .pic.a, .lo, .pic.lo, .so, .dylib, .o or .pic.o).

cc_library(
name = "stdint",
visibility = ["//visibility:public"],
hdrs = [
"include/stdint.h",
],
)

Brian Silverman

unread,
Feb 9, 2016, 10:25:39 PM2/9/16
to Garrett Garcia, bazel-discuss, Austin Schuh, brooks...@gmail.com
That sounds like you have '//cc:sonos_stdint' in the srcs of the dependent rule (//oc/common:upnputil) instead of deps where it belongs. If that's not the problem, can you also show what the declaration of //oc/common:upnputil looks like?

Garrett Garcia

unread,
Feb 9, 2016, 10:40:40 PM2/9/16
to Brian Silverman, bazel-discuss, Austin Schuh, brooks...@gmail.com
Exactly right, I had it in both srcs and deps.  Took it out of srcs and it worked.  Thanks!

wubo...@gmail.com

unread,
Jun 11, 2018, 6:24:01 AM6/11/18
to bazel-discuss
I have the same problem, how to solve

منصة شليلة

unread,
Oct 28, 2019, 1:36:38 AM10/28/19
to bazel-discuss
Reply all
Reply to author
Forward
0 new messages