【问题标题】:How to use Bazel in case of only header files (template classes/function) in C++?在 C++ 中只有头文件(模板类/函数)的情况下如何使用 Bazel?
【发布时间】:2021-09-22 15:35:44
【问题描述】:

我有以下问题: 假设我的项目结构是:

├── project
│   ├── include
|   |   ├── BUILD
|   |   └── library.hpp
│   ├── src
|   |   ├── BUILD
|   |   └── main.cpp
|   ├── test
|   |   ├── BUILD
|   |   └── library_test.cpp
└── WORKSPACE

library.hpp是一个包含模板类实现的文件,它包含在main.cpplibrary_test.cpp中。

如何准备BUILD 文件,以便在编译library_test.cppmain.cpp 时不会出现编译错误:

src/main.cpp:2:10: fatal error: shared_ptr.hpp: No such file or directory
    2 | #include "library.hpp"
      |          ^~~~~~~~~~~~~~~~
compilation terminated.

我尝试的是:

include/BUILD

load("@rules_cc//cc:defs.bzl", "cc_library")

cc_library(
    name = "library",
    srcs = ["library.hpp"],
    includes = ["include"],
    visibility = [
        "//visibility:public",
    ]
)

在上面我还尝试使用hdrstextual_hdrs 而不是srcs

test/BUILD:

load("@rules_cc//cc:defs.bzl", "cc_test")

cc_test(
    name = "library_test",
    srcs = ["library_test.cpp"],
    deps = [
        "@gtest//:gtest",
        "@gtest//:gtest_main",
    ],
    includes = ["include"],
    copts = ["-Iproject/include"],
)

并且要彻底我的WORKSPACE

load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
git_repository(
    name = "gtest",
    remote = "https://github.com/google/googletest",
    branch = "v1.10.x",
)

根据我在互联网上看到的官方 bazel 教程、一些演示文稿或类似问题,我有一个问题要自己解决。它们仅在库中编写的函数定义在 cpp 文件中并且可以编译为目标文件的情况下显示使用 cc_library。

【问题讨论】:

    标签: c++ cmake build googletest bazel


    【解决方案1】:
    # BUILD
    
    cc_library(
        name = "library",
        hdrs= ["include/library.hpp"],
        includes = ["include"],
        visibility = [
            "//visibility:public",
        ]
    )
    
    cc_test(
        name = "library_test",
        srcs = ["test/library_test.cpp"],
        deps = [
            "@gtest//:gtest_main", # gtest_main already contains gtest
            ":library"
        ],
    )
    
    cc_binary(
        name = "binary",
        srcs = ["src/main.cpp"],
        deps = [
            ":library"
        ],
    )
    

    注意:

    • 使用hdrs,因为srcs 对依赖目标不可见。
    • 不要为src/include/test 目录使用单独的BUILD 文件。在 Bazel 中,您应该创建面向功能的包,而不是按层。
    • 在您的代码中,library_test 目标将看不到来自 library 目标的标头,您必须通过 deps 属性将其作为依赖项传递。 Bazel 使用sandboxing TL;DR:操作看不到文件,这些文件未明确定义为依赖项
    • 不要使用copts = ["-Iproject/include"]。当一切都正确完成时,Bazel 会为您完成:在这种情况下,您必须将 includes 属性添加到 library 目标。包含路径将设置在library_test 中,因为测试目标取决于library

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 2021-08-30
      • 1970-01-01
      相关资源
      最近更新 更多