Search code examples
c++bazel

bazel: cannot find dependent header file for very simple case


Firstly, my environment:

  • Windows 10
  • Bazel 2.0.0
  • VS2017

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).


Solution

  • 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".