【问题标题】:Building nlohmann/json as a bazel library gives 'nothing to build' error将 nlohmann/json 构建为 bazel 库会出现“没有构建”错误
【发布时间】:2021-04-26 21:10:16
【问题描述】:

我有一个非常简单的 bazel 项目,我正在尝试将 https://github.com/nlohmann/json 添加为依赖项。为此,我在本地克隆了 json 存储库,并在存储库的根目录中添加了一个 BUILD 文件以生成一个包含单个包含 json.hpp 文件的 cc_library。但是当我构建它时,它总是抱怨没有什么可构建的。

├── 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

我的项目/Main.cpp:

#include <single_include/nlohmann/json.hpp>

int main() {
    ...
}

我的项目/构建:

cc_binary(
  name = "main",
  srcs = ["Main.cpp"],
  deps = [ "//json:json"]
)

myproject构建错误:

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

任何想法我在这里错过了什么?我的目标是在我的 bazel 项目 myproject 中使用 json repo 作为依赖项。

【问题讨论】:

    标签: c++ bazel nlohmann-json


    【解决方案1】:

    最好使用http_repository/git_repository机制来获取外部依赖,因为它们不会弄乱repo的历史,它们更容易更新,甚至不会被获取,如果你不构建任何东西,这取决于它。

    # /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"],
    )
    
    
    # /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/",
    )
    

    然后:

    cc_binary(
      name = "main",
      srcs = ["Main.cpp"],
      deps = [ "@com_github_nlohmann_json//:json"]
    )
    
    // Main.cpp file
    #include "nlohmann/json.hpp"
    

    【讨论】:

    • 这行得通!除了在工作区文件中加载http_archive 模块之外,需要进行的另一个更改是在third_party 文件夹中添加一个空的BUILD 文件,否则bazel 拒绝构建并抱怨没有BUILD 文件。
    【解决方案2】:

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

    另外,cc_library 本身也有output groups。尝试使用 bazel build :json --output_groups=dynamic_library 之类的方法来获取通过构建其源文件生成的库。

    请注意,一般来说,您应该为构建的任何输出创建一个cc_binary。即使您想要一个共享对象,也要创建一个名称以.so 结尾的cc_binary,而不是单独使用cc_library。 dynamic_library 输出不处理传递依赖关系(尽管我猜这对于这个库并不重要,反正它没有依赖关系)。

    【讨论】:

    • output_groups=dynamic_library 似乎什么也没做。但是我在json:BUILD 中添加了strip_include_prefix 并修复了我的主文件中的导入,之后主目标现在可以正常编译了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-18
    • 2019-05-18
    • 2020-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多