this rule is missing dependency declarations for the following files included by 'bcd/hello.c':

2,512 views
Skip to first unread message

Yogur

unread,
Jul 25, 2015, 3:57:01 AM7/25/15
to bazel-discuss
this rule is missing dependency declarations for the following files included by 'bcd/hello.c':
  '/home/yogur/my_project/abc/interface.h'.
but i have 
cc_library(
    name = "hello",
    srcs=["hello.c"],
    copts=["-Iabc"],
)

this error means that  the rule include the .h head file as hello.c #include "interface.h" 
why need i to declar the dependency?

in my project . i need to compile a lot of .c files whose dependency are very different. And all the dependency are in the same folder 
what shold i do ?~
thx for any help~

Thiago Farina

unread,
Jul 25, 2015, 12:58:31 PM7/25/15
to Yogur, bazel-discuss
On Sat, Jul 25, 2015 at 4:57 AM, Yogur <yog...@gmail.com> wrote:
this rule is missing dependency declarations for the following files included by 'bcd/hello.c':
  '/home/yogur/my_project/abc/interface.h'.
but i have 
cc_library(
    name = "hello",
    srcs=["hello.c"],
    copts=["-Iabc"],
)

So you have the following:

$ cat /my_project/bcd/BUILD
cc_library(
  name= "hello",
  srcs=["hello.c"],
  copts=["-labc"],
)

Then interface.h is in /my_project/abc/, right?

How does it look like your abc BUILD file?
$ cat /my_project/abc/BUILD
?

I think in Bazel you have to be explicit about your dependencies.


--
Thiago Farina

Yogur

unread,
Jul 26, 2015, 11:22:27 PM7/26/15
to bazel-discuss, tfa...@chromium.org
 Yes, interface.h is in /my_project/abc  there is no BUILD file.
the rule haved got it,and my .c file include the .h, why still I need to declar the dependency?
I mean , what the consideration that bazel do this ?
my temporary way to solve the problem is 
define a filegroup whose srcs is interface.h in the /my_project/abc/BUILD
then add the deps=["//my_project/abc:hello_deps"]  
it works,but i have to do the similar work many times.
so i want to konw if there is a simple solution?

David Chen

unread,
Jul 27, 2015, 4:10:53 AM7/27/15
to Yogur, bazel-discuss, Thiago Farina
To confirm, from what I understand, your source tree currently looks like the following:

my_project/
  WORKSPACE
  abc/
    interface.h
  bcd/
    BUILD
    hello.c

In my_project/bcd/BUILD, you currently have:

cc_library(
    name = "hello",
    srcs = ["hello.c"],
    copts = ["-Iabc"],
)

And in my_project/bcd/hello.c, you currently have:

#include "interface.h"

Please let me know if I am missing anything.

------------------

Here are a few points to keep in mind about building C/C++ code with Bazel:
  • The default include path for header files is the root of your source tree. As a result, in my_project/bcd/hello.c, your include should be:
    #include "abc/interface.h". However, this can be changed using the includes attribute of the cc_* rules.
  • Usually, you should not use -I and -l flags directly to specify dependencies in your source tree. Instead, you should try to use deps when possible.
  • If you haven't already, I highly recommend reading Bazel's C++ Basics documentation, which provides good explanations on building C/C++ projects with Bazel.
You have a few options here:

Option 1: Add a BUILD file for abc (recommended)

Your source tree would look like this:

my_project/
  WORKSPACE
  abc/
    BUILD
    interface.h
  bcd/
    BUILD
    hello.c

abc/BUILD:

cc_library(
    name = "abc",
    srcs = ["interface.h"],
    visibility = ["//visibility:public"],
)

bcd/hello.c:

#include "abc/interface.h"
...

In bcd/BUILD, specify a dependency on abc as follows:

cc_library(
    name = "hello",
    srcs = ["hello.c"],
    deps = ["//abc"],
)

This is the recommended approach because it provides a cleaner separation between different packages in your source tree, and it is more clear which library you are including different headers from.

Option 2: Use a common BUILD file at the root of your project

Your source tree would look like the following:

my_project/
  WORKSPACE
  BUILD
  abc/
    interface.h
  bcd/
    hello.c

In your root BUILD file, you can use the includes attribute to tell the rule to look for headers under abc/:

cc_library(
    name = "hello",
    srcs = ["bcd/hello.c"],
    includes = ["abc"],
)

bcd/hello.c:

#include "interface.h"
...

While this works, it is less ideal because there is a less clear separation between different modules.

In any case, I recommend reading the C++ Basics documentation if you haven't already. Keep in mind that many of the details there also apply to building C code since the cc_* rules can be used for building both C and C++ code.

Feel free to let me know if you have any more questions.

--
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/fce08f72-2c15-4adb-985b-1aa532bf053c%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Yogur

unread,
Jul 27, 2015, 7:04:05 AM7/27/15
to bazel-discuss, tfa...@chromium.org, d...@google.com
thank you very much!!!
I have read the C++ Basics documentation many times,and I have tried your two opitions.
my task is thanslate makefile to bazel BUILD , so i  can't  change the .c files。
  Each string of includes is prepended with -I and added to COPTS,so i think they have no difference.
the main reason of the way i choose is I care less the separation between different packages in  my source tree
I want to simpify the code to use copts  :)

now I have a deeper understand the difference of Bazel and Makefile
my way is define a filegroup containg the include .h files  in  abc/BUILD ,and add  the filegroup to the rule srcs,
Reply all
Reply to author
Forward
0 new messages