【问题标题】:How can I link a system library statically in Bazel?如何在 Bazel 中静态链接系统库?
【发布时间】:2017-09-11 00:01:39
【问题描述】:

如何以静态模式 (linkstatic=1) 静态链接系统库?我尝试使用“-Wl,-Bstatic -lboost_thread -Wl,-Bdynamic”或“-Wl,-Bstatic”、“-lboost_thread”、“-Wl,-Bdynamic”,但它们都不起作用。我不想在系统中硬编码 libboost_thread.a 的路径。

cc_binary(
    name = "main",
    srcs = [
        "main.cpp",
    ],
    linkopts = [
        "-lboost_thread",
    ],
)

并且 boost_thread 库是作为动态库链接的。

ldd bazel-bin/main
linux-vdso.so.1
libboost_thread.so.1.54.0 => /usr/lib/x86_64-linux-gnu/libboost_thread.so.1.54.0
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6
...

【问题讨论】:

    标签: c++ bazel


    【解决方案1】:

    在您的 WORKSPACE 文件中定义一个外部存储库...

    new_local_repository(
        name = "boost_thread",
        path = "/usr/lib/x86_64-linux-gnu",
        build_file = "boost_thread.BUILD"
    )
    

    创建 boost_thread.BUILD 文件

    cc_library(
       name = "lib",
       srcs = ["libboost_thread.a"],
       visibility = ["//visibility:public"],
    )
    

    然后在你的 cc_binary 规则中添加

    deps = ["@boost_thread//:lib",],
    

    然后扔一个

    linkstatic = 1
    

    为了安全起见。

    【讨论】:

      【解决方案2】:

      根据这个问题的答案,Telling gcc directly to link a library statically, "-l:libboost_thread.a" 会静态链接系统库,不会硬编码系统中libboost_thread.a的路径。

      cc_binary(
          name = "main",
          srcs = [
              "main.cpp",
          ],
          linkopts = [
              "-l:libboost_thread.a",
              "-l:libboost_system.a",
          ],
      )
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-12-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多