C++ 将nlohmann/json构建为bazel库提供了';无需构建';错误

C++ 将nlohmann/json构建为bazel库提供了';无需构建';错误,c++,bazel,nlohmann-json,C++,Bazel,Nlohmann Json,我有一个非常简单的bazel项目,我试图将其添加为依赖项。为此,我在本地克隆了jsonrepo,并在repo的根目录中添加了一个BUILD文件,以生成一个cc_库,其中包含单个includejson.hpp文件。但当我构建它时,它总是抱怨没有什么可构建的 ├── json │   ├── BUILD │   ├── // all files under nlohmann/json repo. ├── myproject │   ├── BUILD │   └── Main.cpp └── WOR

我有一个非常简单的bazel项目,我试图将其添加为依赖项。为此,我在本地克隆了
json
repo,并在repo的根目录中添加了一个
BUILD
文件,以生成一个
cc_库
,其中包含单个include
json.hpp
文件。但当我构建它时,它总是抱怨没有什么可构建的

├── json
│   ├── BUILD
│   ├── // all files under nlohmann/json repo.
├── myproject
│   ├── BUILD
│   └── Main.cpp
└── WORKSPACE
json/构建:

cc_library(
    name = "json",
    hdrs = ["single_include/nlohmann/json.hpp"],
    includes = ["json"],
    visibility = ["//visibility:public"],
)
构建上述库成功,但没有生成库。注意输出中的“(无需构建)”消息

bazel build :json
INFO: Analyzed target //json:json (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //json:json up-to-date (nothing to build)
INFO: Elapsed time: 0.065s, Critical Path: 0.00s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
myproject/Main.cpp:

#include <single_include/nlohmann/json.hpp>

int main() {
    ...
}
myproject
生成错误:

myproject/Main.cpp:1:44: fatal error: single_include/nlohmann/json.hpp: No such file or directory
compilation terminated.

你知道我在这里遗漏了什么吗?我的目标是将
json
repo作为我的bazel项目
myproject
中的依赖项使用,问题是
cc_库
没有,因此当您在命令行上指定其目标时,bazel不知道要构建什么。尝试
bazel build:main
来构建二进制文件

而且,
cc\u库本身也有。尝试类似于
bazel build:json--output_groups=dynamic_library
的方法来获取构建源文件所产生的库


请注意,一般来说,您应该为生成的任何输出创建一个。即使您想要共享对象,也要创建一个名称以
结尾的
cc\u二进制文件
。因此
而不是单独使用
cc\u库
dynamic_库
输出不处理可传递的依赖项(尽管我想这对这个库来说并不重要,因为它没有依赖项)。

问题是
cc_库
没有,所以当您在命令行上指定它的目标时,Bazel不知道要构建什么。尝试
bazel build:main
来构建二进制文件

而且,
cc\u库本身也有。尝试类似于
bazel build:json--output_groups=dynamic_library
的方法来获取构建源文件所产生的库


请注意,一般来说,您应该为生成的任何输出创建一个。即使您想要共享对象,也要创建一个名称以
结尾的
cc\u二进制文件
。因此
而不是单独使用
cc\u库
dynamic_库
输出不处理可传递的依赖项(尽管我想这对这个库来说并不重要,因为它没有依赖项)。

最好使用
http_库
/
git_库
机制获取外部依赖项,因为它们不会扰乱回购协议的历史,所以更新它们更容易,而且如果您不构建任何依赖于它的东西,它们甚至不会被提取

#/WORKSPACE文件
http\u档案(
name=“com\u github\u nlohmann\u json”,
build_file=“//第三方:json.build”,#见下文
sha256=“4cf0df69731494668bdd6460ed8cb269b68de9c19ad8c27abc24cd72605b2d5b”,
strip_prefix=“json-3.9.1”,
URL=[”https://github.com/nlohmann/json/archive/v3.9.1.tar.gz"],
)
#/third_party/json.BUILD文件
包(默认_可见性=[“//可见性:公共”])
加载(“@rules\u cc//cc:defs.bzl”,“cc\u库”)
许可证([“注意])#麻省理工学院许可证
图书馆(
name=“json”,
hdrs=[“single_include/nlohmann/json.hpp”],
strip_include_prefix=“single_include/”,
)
然后:

cc\u二进制文件(
name=“main”,
srcs=[“Main.cpp”],
deps=[“@com\u github\u nlohmann\u json/:json”]
)
//Main.cpp文件
#包括“nlohmann/json.hpp”

最好使用
http\u repository
/
git\u repository
机制获取外部依赖项,因为它们不会扰乱repo的历史记录,更容易更新,如果不构建依赖它的任何内容,甚至不会获取外部依赖项

#/WORKSPACE文件
http\u档案(
name=“com\u github\u nlohmann\u json”,
build_file=“//第三方:json.build”,#见下文
sha256=“4cf0df69731494668bdd6460ed8cb269b68de9c19ad8c27abc24cd72605b2d5b”,
strip_prefix=“json-3.9.1”,
URL=[”https://github.com/nlohmann/json/archive/v3.9.1.tar.gz"],
)
#/third_party/json.BUILD文件
包(默认_可见性=[“//可见性:公共”])
加载(“@rules\u cc//cc:defs.bzl”,“cc\u库”)
许可证([“注意])#麻省理工学院许可证
图书馆(
name=“json”,
hdrs=[“single_include/nlohmann/json.hpp”],
strip_include_prefix=“single_include/”,
)
然后:

cc\u二进制文件(
name=“main”,
srcs=[“Main.cpp”],
deps=[“@com\u github\u nlohmann\u json/:json”]
)
//Main.cpp文件
#包括“nlohmann/json.hpp”

这很有效!除了在工作区文件中加载
http\u存档
模块外,还需要做另一个更改,即在
第三方
文件夹中添加一个空的构建文件,否则bazel拒绝构建并抱怨没有构建文件。这很有效!除了在工作区文件中加载
http\u archive
模块外,还需要做另一个更改,即在
third\u party
文件夹中添加一个空的构建文件,否则bazel拒绝构建并抱怨没有构建文件。
output\u groups=dynamic\u library
似乎什么都没做。但是我在
json:BUILD
中添加了
strip\u include\u prefix
,并修复了主文件中的导入,之后主目标现在可以编译了。
output\u groups=dynamic\u library
似乎什么都没做。但是我在
json:BUILD
中添加了
strip\u include\u prefix
,并修复了主文件中的导入,之后主目标现在可以正常编译了。
myproject/Main.cpp:1:44: fatal error: single_include/nlohmann/json.hpp: No such file or directory
compilation terminated.