How to Expose Library Header Files to cc_binary Without Using copts in the cc_binary

39 views
Skip to first unread message

Penugonda chenna reddy

unread,
Nov 4, 2024, 9:16:23 AMNov 4
to bazel-discuss

Hi all,

I'm having trouble with a Bazel cc_binary target that depends on a cc_library. Specifically, I expected the headers of the cc_library to be automatically accessible to the cc_binary, but I find that I have to manually specify the include path in the cc_binary’s copts for it to work. Here’s a simplified example of my setup:

python
Copy code
cc_library( name = "hello-greet", 
                   srcs = ["hello-greet.cc"],
                   hdrs = ["include/hello-greet.h"],
                   copts = ["-Imain/include"], # I expected this copts to work for hello-world too                        includes = ["main/include"], # Tried includes, but the path still isn't accessible  ) 
 cc_binary( name = "hello-world", 
                     srcs = ["hello-world.cc"],
                     deps = [":hello-greet"], 
# Adding     copts = ["-Imain/include"] here makes it work, but I'd like to avoid that
)

In this setup, I was expecting that the hello-world binary would have access to hello-greet.h (which is located in main/include) without needing copts in the cc_binary rule itself. However, without specifying copts = ["-Imain/include"] directly in hello-world, it doesn't seem to work.

My question is:

  1. Is there a way to automatically expose the headers from hello-greet to hello-world without duplicating copts in each binary that depends on it?
  2. Shouldn’t the includes attribute in hello-greet already provide this behavior by propagating the include path to dependent targets?

Any insights or best practices on how to handle this would be greatly appreciated!

Thank you!

 PCReddy

Filip Filmar

unread,
Nov 4, 2024, 2:10:42 PMNov 4
to Penugonda chenna reddy, bazel-discuss
On Mon, Nov 4, 2024 at 6:16 AM Penugonda chenna reddy <penug...@gmail.com> wrote:

I'm having trouble with a Bazel cc_binary target that depends on a cc_library. Specifically, I expected the headers of the cc_library to be automatically accessible to the cc_binary, but I find that I have to manually specify the include path in the cc_binary’s copts for it to work.

This should not be necessary.

Any insights or best practices on how to handle this would be greatly appreciated!

I suspect this has to do with how you are referring to the header file.  Typically you would do `#include "the/entire/path/from/the/root/include.h"
Do you have a link to the sample repo?

F

Keith Smiley

unread,
Nov 5, 2024, 3:20:13 AMNov 5
to Filip Filmar, Penugonda chenna reddy, bazel-discuss
See also the `includes` attribute in the cc_library docs

--
Keith Smiley


--
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 visit https://groups.google.com/d/msgid/bazel-discuss/CAKaOXii3jQrfjkCuwSJGRhR_tfxHD8Sca9wrUfEeUp3L_59YaQ%40mail.gmail.com.

Victory RF

unread,
Nov 5, 2024, 3:23:14 AMNov 5
to Filip Filmar, Penugonda chenna reddy, bazel-discuss

What will be my commission or reward if I work this out for you


Victory RF

unread,
Nov 7, 2024, 4:38:03 AMNov 7
to Keith Smiley, Filip Filmar, Penugonda chenna reddy, bazel-discuss

I have the program in my store file but I'll charge you for that , I don't know if you can come up with $360


Adam Hamilton

unread,
Nov 7, 2024, 5:36:51 AMNov 7
to bazel-discuss
When I use Bazel for work, we do not specify an includes attribute, which means every header include is relative to the workspace root, where your WORKSPACE file is located. It takes some getting used to but with modern IDEs, you can just copy the relative path location and paste it right into your include statement.

Now, from the way you are trying to structure your BUILD file, it appears you just want to do #include "hello-greet.h" so you should, I believe use includes=["include"] in your cc_library rule. You can remove the copts attribute here as I suspect it wasn't really doing anything.

When you used includes=["main/include"] it would have been relative to your BUILD file location so of course this would never work.

You should not require the copts attribute for the cc_binary rule now. Give it try and hopefully, it will start working for you.

Cheers

Penugonda chenna reddy

unread,
Nov 12, 2024, 4:46:02 AMNov 12
to Keith Smiley, Filip Filmar, bazel-discuss
Thanks Keith Smiley.. It worked !! Understood the difference between includes and copts.. 
Reply all
Reply to author
Forward
0 new messages