Ran*_*ion 1 c++ bazel nlohmann-json
我有一个非常简单的 bazel 项目,我试图将https://github.com/nlohmann/json添加为依赖项。为了实现这一目标,我json在本地克隆了存储库,并BUILD在存储库的根目录中添加了一个文件以生成cc_library包含单个包含文件的json.hpp文件。但当我构建它时,它总是抱怨没有什么可构建的。
\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 json\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 BUILD\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 // all files under nlohmann/json repo.\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 myproject\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 BUILD\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 Main.cpp\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 WORKSPACE\nRun Code Online (Sandbox Code Playgroud)\njson/构建:
\ncc_library(\n name = "json",\n hdrs = ["single_include/nlohmann/json.hpp"],\n includes = ["json"],\n visibility = ["//visibility:public"],\n)\nRun Code Online (Sandbox Code Playgroud)\n上述构建成功,但没有生成库。请注意输出中的“(没有任何内容可构建)”消息。
\nbazel build :json\nINFO: Analyzed target //json:json (0 packages loaded, 0 targets configured).\nINFO: Found 1 target...\nTarget //json:json up-to-date (nothing to build)\nINFO: Elapsed time: 0.065s, Critical Path: 0.00s\nINFO: 1 process: 1 internal.\nINFO: Build completed successfully, 1 total action\nRun Code Online (Sandbox Code Playgroud)\n我的项目/Main.cpp:
\n#include <single_include/nlohmann/json.hpp>\n\nint main() {\n ...\n}\nRun Code Online (Sandbox Code Playgroud)\n我的项目/构建:
\ncc_binary(\n name = "main",\n srcs = ["Main.cpp"],\n deps = [ "//json:json"]\n)\nRun Code Online (Sandbox Code Playgroud)\nmyproject构建错误:
myproject/Main.cpp:1:44: fatal error: single_include/nlohmann/json.hpp: No such file or directory\ncompilation terminated.\nRun Code Online (Sandbox Code Playgroud)\n我在这里缺少什么想法吗?我的目标是使用该json存储库作为我的 bazel 项目中的依赖项myproject。
最好使用http_repository/git_repository机制获取外部依赖项,因为它们不会扰乱存储库的历史记录,它们更容易更新,并且如果您不构建任何依赖于它的东西,它们甚至不会被获取。
# /WORKSPACE file
http_archive(
name = "com_github_nlohmann_json",
build_file = "//third_party:json.BUILD", # see below
sha256 = "4cf0df69731494668bdd6460ed8cb269b68de9c19ad8c27abc24cd72605b2d5b",
strip_prefix = "json-3.9.1",
urls = ["https://github.com/nlohmann/json/archive/v3.9.1.tar.gz"],
)
Run Code Online (Sandbox Code Playgroud)
# /third_party/json.BUILD file
package(default_visibility = ["//visibility:public"])
load("@rules_cc//cc:defs.bzl", "cc_library")
licenses(["notice"]) # MIT license
cc_library(
name = "json",
hdrs = ["single_include/nlohmann/json.hpp"],
strip_include_prefix = "single_include/",
)
Run Code Online (Sandbox Code Playgroud)
进而:
cc_binary(
name = "main",
srcs = ["Main.cpp"],
deps = [ "@com_github_nlohmann_json//:json"]
)
Run Code Online (Sandbox Code Playgroud)
// Main.cpp file
#include "nlohmann/json.hpp"
Run Code Online (Sandbox Code Playgroud)