【问题标题】:Include <headers.h> installed in non standard location包括安装在非标准位置的 <headers.h>
【发布时间】:2018-04-20 20:40:14
【问题描述】:

我目前正在使用第三方库,该库的标头使用尖括号声明,例如标准库:

#include <header.h>

但是,这些标头安装在非标准位置,例如/opt/company/software/version/part_software/include

对于像 MAKE 这样更传统的构建器,我可以只使用 CXXFLAGS 指示 g++ 也在此文件夹中查找库,这最终归结为将 -I/opt/company/software/version/part_software/include 选项传递给 g++。

当尝试在 bazel 中使用 copts = [ "-I/opt/company/software/version/part_software/include" ] 执行相同操作时,我收到“path outside of the execution root”错误。

据我了解,bazel 不喜欢安装 lib 的位置,因为构建需要可重现,并且包含位于执行根之外的库违反了此约束。

我带来的一个丑陋的技巧是在 /usr/local/include 中创建标题的符号链接,并在 bazel 构建中使用 copts = [ "-I/usr/local/include" ]。但是,我发现这种方法非常 hacky,并且我想找到一种更笨拙的方法来解决这个问题。


注意:我无法在 bazel 构建期间安装该程序,因为它使用了一个我无法控制的封闭式安装程序。此安装程序无法在 bazel 的沙盒环境中运行,因为它需要在环境中无法访问的某些路径上进行写入。

【问题讨论】:

  • 我确信有构建选项可以修复它,但我不知道 bazel :-)。如果一切都失败了,您能否将项目文件夹中的符号链接添加到已安装的文件夹中?或者从其他可以设置路径的地方。
  • 我不知道bazel,但是在文档网站上可以找到关于第三部分代码的内容:docs.bazel.build/versions/master/bazel-and-cpp.html

标签: c++11 bazel


【解决方案1】:

所以,事实证明,包含第三方库的 bazelesque 方式只是创建封装库的包。

感谢这个有用的discussion,我已经设法用我的第三方库创建了一个包。

首先我们需要一个BUILD文件,这里命名为package_name.BUILD

package(
    default_visibility = ["//visibility:public"]
)

cc_library(
    name = "third_party_lib_name", #name to reference the third party library in other BUILD files
    srcs = [
        "external/soft/lib/some_lib.so", #.so files to include in the lib
        "software/lib/os/arch/lib_some_plugin.so",
    ],
    hdrs = glob([ # the glob takes all the headers needed
        "software/include/**/*.h",
        "software/include/**/*.hpp",
    ]), 
    includes = ["software/include/"], # Specify which files are included when we use the library
)

现在我们需要在 WORKSPACE 文件中引用 lib 一个子模块:

  new_local_repository(
      name = "package_name",
      path = "opt/company/software/version",
      # build_file: path to the BUILD file, here in the same directory that the main WORKSPACE one
      build_file = __workspace_dir__ + "/package_name.BUILD", 
      )

现在,我不再使用copt 来引用所需的标头,而是在需要时在cc_rule 的deps 中添加一行,例如:

cc_library(
    name="some_internal_lib",
    srcs = ["some_internal_lib.cc"],
    deps = [
         "@package_name//:third_party_lib_name", #referencing the third party lib
           ],
)

【讨论】:

    猜你喜欢
    • 2019-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-15
    • 1970-01-01
    • 1970-01-01
    • 2010-10-25
    • 1970-01-01
    相关资源
    最近更新 更多