jiandingzhe
jiandingzhe

Reputation: 2121

bazel: cannot find dependent header file for very simple case

Firstly, my environment:

I made a very simple C++ project using bazel. It has two BUILDs to test target referencing. The layout is:

- WORKSPACE
- libfoo
  |- BUILD
  |- foo.h
  |- foo.cpp
- bar
  |- BUILD
  |- bar.cpp

In BUILD file of libfoo, it defined a very simple library:

cc_library(
    name = "foo",
    srcs = ["foo.cpp"],
    hdrs = ["foo.h"],
    visibility = ["//visibility:public"]
)

And in bar's BUILD file, it declared an executable that deps libfoo:

cc_binary(
    name = "bar",
    srcs = ["bar.cpp"],
    deps = ["//libfoo:foo"],
)

, where bar.cpp called a function defined in libfoo:

#include "foo.h"

#include <iostream>

int main()
{
    std::clog << "bar main" << std::endl;
    say_foo(); // a function defined in libfoo
}

However when I compile bar using bazel build "//bar:bar", the compiler claims foo.h cannot be opened (error code C1083).

Upvotes: 1

Views: 2020

Answers (2)

filippo
filippo

Reputation: 146

You have two ways of solving it:

Either you specify the includes property in cc_library

cc_library(
    name = "foo",
    srcs = ["foo.cpp"],
    hdrs = ["foo.h"],
    includes = ["./"],
    visibility = ["//visibility:public"]
)

Or you in bar.cpp you include foo.h as #include "libfoo/foo.h".

Upvotes: 4

jiandingzhe
jiandingzhe

Reputation: 2121

After more detailed view on official tutorial, I found that I misunderstood the function of deps, it does not add library's dir into inc dir.

Actually, #include "libfoo/foo.h" should be written in bar.cpp instead of #include "foo.h", where the full relative path of target foo must be used.

Upvotes: 2

Related Questions