Google Test header file issues: No such file or directory

27 views
Skip to first unread message

Murat Gozu

unread,
May 23, 2024, 2:02:54 PMMay 23
to bazel-discuss
Hi everyone,

I am new at google test, and just wanted to do a simple test by verifying a function that calculates the power of the number. The function are declared and defined in different header file and different source file. Here is the part of the code:
header.hpp:
int pow_test(int x, int y);

src1.c:
#include <math.h>
#include "header.hpp"

int pow_test(int x, int y){
  return pow(x,y);
}

test.cc:
#include <gtest/gtest.h>
#include <math.h>
#include "src/header.hpp"

TEST (TestingPowTest, PowTests){
  EXPECT_EQ(pow_test(2,4),16);
}

I am receiving error message when I try to build the code with the command line below:
   bazel test --cxxopt=-std=c++14 --test_output=all //:test 

The error message:
test.cc:4:10: fatal error: src/header.hpp: No such file or directory
    4 | #include "src/header.hpp"

But the header file is the same directory, why it can not find it? Can anybody help me on that? Thank you

Allan Clark

unread,
May 23, 2024, 9:44:31 PMMay 23
to bazel-discuss
You need to look at the rule you’re using to compile the C++ code: you haven’t included the BUILD or BUILD.bazel file so I cannot confirm that.

Essentially, bazel works in isolated directories.  You need to note both .cc and .hpp files as “srcs=“ so that bazel knows you need them available at build time.  The .cc will be compiled, the .hpp is simply made available during compilation.

Alternatively, note that you have included this header as “header.hpp” in one case, “src/header.hpp” in another.  You’re either working in different directories, or “src/“ is incorrect there.

Create a small github repo, and check in your example; ping us, and/or me directly, I’m very happy to take a look at your example and propose a PR that fixes.

Allan

Filip Filmar

unread,
May 23, 2024, 10:36:13 PMMay 23
to Allan Clark, bazel-discuss
On Thu, May 23, 2024 at 6:44 PM Allan Clark <all...@chickenandpork.com> wrote:
Essentially, bazel works in isolated directories.  You need to note both .cc and .hpp files as “srcs=“ so that bazel knows you need them available at build time.
I am receiving error message when I try to build the code with the command line below:
   bazel test --cxxopt=-std=c++14 --test_output=all //:test 

The error message:
test.cc:4:10: fatal error: src/header.hpp: No such file or directory
    4 | #include "src/header.hpp"

But the header file is the same directory, why it can not find it? Can anybody help me on that? Thank you

Indeed - this is probably the number one reason for the "file not found" message when working with bazel.

One important thing to know is that, indeed, bazel does not compile your code in place as many other build tools do by default. Instead it makes a "copy" (symlinks and may sandbox and such), and compiles only what's visible there. This means your BUILD.bazel file must explicitly say that you wanted your `src/header.cpp` to be visible in that compilation. Else you get a "file not found".  This is an excellent feature when compiling large programs; not so much when you are just starting out and have a small example that you want to confirm runs.

And indeed - a publicly accessible repository that showcases your problem would be most helpful for diagnosis.

Here's something that looks similar to what OP seems to be asking for. https://stackoverflow.com/questions/45814669/c-project-with-bazel-and-gtest, and the associated repository https://github.com/vincent-picaud/Bazel_with_GTest

I remain puzzled why sending minimal examples isn't the norm for bazel-discuss.

Cheers,
F

Filip Filmar

unread,
May 23, 2024, 10:41:59 PMMay 23
to Murat Gozu, bazel-discuss
On Thu, May 23, 2024 at 11:02 AM Murat Gozu <mura...@gmail.com> wrote:
The error message:
test.cc:4:10: fatal error: src/header.hpp: No such file or directory
    4 | #include "src/header.hpp"

But the header file is the same directory, why it can not find it? Can anybody help me on that? Thank you

As written before, the answer lies in your BUILD.bazel file.  When compiling with bazel, all source files, including headers, *must* be explicitly mentioned in the BUILD.bazel file to be visible to the process that runs the compilation, because bazel makes a sandbox for the compile action, and only brings in the files that are explicitly requested. 

That is, I would expect your target to mention `hrds` and `srcs` like here: https://github.com/vincent-picaud/Bazel_with_GTest/blob/master/MyLib/BUILD.

Except, I think that `glob(...)` calls should not be used in large programs, as they lead to well documented compilation efficiency issues. I suspect that if you have a "hello gtest" program, it doesn't matter.

F
 
Reply all
Reply to author
Forward
0 new messages