具有数据存储库的C ++ Bazel项目 [英] C++ Bazel project with a Data repository
问题描述
我有一个(基本)C ++项目:
I have a (basic) C++ project:
├── bin
│ ├── BUILD
│ ├── example.cpp
├── data
│ └── someData.txt
└── WORKSPACE
其中可执行文件 example.cpp 使用data/
目录中的一些数据文件:
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;
}
我的Bazel设置是最小的设置:
My Bazel setup is the minimal one:
- WORKSPACE :空文件
- bin/BUILD :
cc_binary(name = "example",srcs = ["example.cpp"])
- data/someData.txt :包含
Hello_world!
行
- WORKSPACE: empty file
- bin/BUILD:
cc_binary(name = "example",srcs = ["example.cpp"])
- data/someData.txt: contains the
Hello_world!
line
问题是Bazel将所有这些文件移动到特殊位置:
The problem is that Bazel moves all these files in special locations:
.
├── bazel-Bazel_with_Data -> ...
├── bazel-bin -> ...
├── bazel-genfiles -> ...
├── bazel-out -> ...
├── bazel-testlogs -> ...
示例特有的可执行文件找不到 data/someData.txt 文件:
In peculiar the example executable will not found the data/someData.txt file:
bazel run bin:example
将打印:
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
问题是如何进行管理?
我希望示例可执行文件能够找到 Data/someData.txt 文件.
I want the example executable to be able to find the Data/someData.txt file.
推荐答案
注意::此解决方案似乎无法在Windows下运行(请参见注释).
CAVEAT: it seems this solution does not work under Windows (see comments).
必须在 data 目录中创建一个额外的 BUILD 文件,该文件定义必须导出的数据文件.现在的项目结构为:
One must create an extra BUILD file in the data directory that defines what data files must be exported. The project structure is now:
├── bin
│ ├── BUILD
│ ├── example.cpp
├── data
│ ├── BUILD
│ └── someData.txt
└── WORKSPACE
此新的数据/内置文件为:
exports_files(["someData.txt"])
然后修改了 bin/BUILD 文件以添加 someData.txt 依赖项:
And the bin/BUILD file is modified to add the someData.txt dependency:
cc_binary(
name = "example",
srcs = ["example.cpp"],
data = ["//data:someData.txt"],
)
现在,如果您运行:
bazel run bin:example
您应该得到:
INFO: Analysed target //bin:example (2 packages loaded).
INFO: Found 1 target...
Target //bin:example up-to-date:
bazel-bin/bin/example
INFO: Elapsed time: 0.144s, Critical Path: 0.01s
INFO: Build completed successfully, 3 total actions
INFO: Running command line: bazel-bin/bin/example
Hello_world!
表示示例可执行文件已找到 data/someData.txt 文件并打印了其内容.
meaning that the example executable has found the data/someData.txt file and printed its content.
还请注意,您可以对
cc_test(...,data =["//data:someData.txt"], )
您可以从 GitHub存储库中复制此注释.
You can reproduce this note from this GitHub repo.
这篇关于具有数据存储库的C ++ Bazel项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!