I have a (basic) C++ project:
├── bin
│ ├── BUILD
│ ├── example.cpp
├── data
│ └── someData.txt
└── WORKSPACE
where the executable example.cpp use some data files from data/
directory:
#include <fstream>
#include <iostream>
int main()
{
std::ifstream in("data/someData.txt");
if (!in)
{
std::cerr << "Can not open file!";
return EXIT_FAILURE;
}
std::string message;
if (!(in >> message))
{
std::cerr << "Can not read file content!";
return EXIT_FAILURE;
}
std::cout << message << std::endl;
return EXIT_SUCCESS;
}
My Bazel setup is the minimal one:
- WORKSPACE: empty file
- bin/BUILD:
cc_binary(name = "example",srcs = ["example.cpp"])
- data/someData.txt: contains the
Hello_world!
line
The problem is that Bazel moves all these files in special locations:
.
├── bazel-Bazel_with_Data -> ...
├── bazel-bin -> ...
├── bazel-genfiles -> ...
├── bazel-out -> ...
├── bazel-testlogs -> ...
In peculiar the example executable will not found the data/someData.txt file:
bazel run bin:example
will print:
INFO: Analysed target //bin:example (0 packages loaded).
INFO: Found 1 target...
Target //bin:example up-to-date:
bazel-bin/bin/example
INFO: Elapsed time: 0.101s, Critical Path: 0.00s
INFO: Build completed successfully, 1 total action
INFO: Running command line: bazel-bin/bin/example
Can not open file!ERROR: Non-zero return code '1' from command: Process exited with status 1
The question is how to manage this?
I want the example executable to be able to find the Data/someData.txt file.