Bazel 在使用 C++ 标准库的 AWS Ubuntu 上构建失败并包含路径错误

问题描述 投票:0回答:1

我有一个在 Windows 上成功构建的 C++ Bazel 项目,但是当我尝试使用 Bazel 在 AWS Ubuntu 上构建相同的项目时,遇到了几个错误。该项目使用 nlohmann/json 库进行 JSON 处理,并使用 Abseil 库。以下是构建输出中的关键错误消息:

  1. 对于
    nlohmann/json.hpp
main.cc:12:10: fatal error: nlohmann/json.hpp: No such file or directory
   | #include <nlohmann/json.hpp>
   |          ^~~~~~~~~~~~~~~~~~~
compilation terminated.

  1. 对于来自绳降的
    graphcycles.cc
external/com_google_absl/absl/synchronization/internal/graphcycles.cc: In member function 'void absl::synchronization_internal::GraphCycles::RemoveNode(void*)':
external/com_google_absl/absl/synchronization/internal/graphcycles.cc:451:26: error: 'numeric_limits' is not a member of 'std'
  | if (x->version == std::numeric_limits<uint32_t>::max()) {

nlohmann/json.hpp 文件存在于我的项目根目录中名为 nlohmann 的文件夹中,并且我在 Bazel BUILD 文件中设置了 cc_library 规则以包含此目录。构建在 Windows 上运行良好,但在 AWS Ubuntu 上会出现这些错误。

这是我的构建文件的相关部分:

cc_library(
    name = "json",
    hdrs = glob(["nlohmann/**/*.hpp"]),
    visibility = ["//visibility:public"] )

cc_binary(
    name = "main",
    srcs = ["main.cc"],
    deps = [...],
    copts = ["/std=c++17"],  # Note: this option works on Windows )

我怀疑这个问题可能与 Windows 和 Linux 之间处理包含路径或 C++ 标准版本的差异有关。任何关于如何解决这些错误以在 AWS Ubuntu 上成功构建的见解或建议将不胜感激。

c++ linux ubuntu build bazel
1个回答
0
投票

您可以创建一个

.bazelrc
:

# GCC 11.3
build:gcc11 --cxxopt=-std=c++20

# Clang 14.0.0
build:clang14 --cxxopt=-std=c++20

# Visual Studio 2022
build:vs2022 --cxxopt=/std:c++20

删除

copts = ["/std=c++17"],
并使用特定配置进行构建,例如
bazel build --config=gcc11 //...
bazel build --config=vs2022 //...

您还可以使用

linux
macos
windows
的三个“预定”配置。可以通过将
build --enable_platform_specific_config
添加到您的
.bazelrc
文件来启用此功能。

然后您可以为不同平台定义默认值,例如:

build:macos --cxxopt=-std=c++2b # Use this standard on macOS
build:windows ... # Windows goes here

更干净的解决方案是像 Abseil-cpp 一样使用

config_setting
,例如:

config_setting(
    name = "gcc_compiler",
    flag_values = {
        "@bazel_tools//tools/cpp:compiler": "gcc",
    },
    visibility = [":__subpackages__"],
)

其他地方 - 使用选择来定义编译器标志:

ABSL_TEST_COPTS = select({
    "//absl:msvc_compiler": ABSL_MSVC_TEST_FLAGS,
    "//absl:clang-cl_compiler": ABSL_CLANG_CL_TEST_FLAGS,
    "//absl:clang_compiler": ABSL_LLVM_TEST_FLAGS,
    "//absl:gcc_compiler": ABSL_GCC_TEST_FLAGS,
    "//conditions:default": ABSL_GCC_TEST_FLAGS,
})

在一些测试中:

cc_test(
    ...
    copts = ABSL_TEST_COPTS,
    ...
)

请参阅 Abseil-cpp 源代码。

© www.soinside.com 2019 - 2024. All rights reserved.