gpt4 book ai didi

c++ - 如何在 Bazel 中静态链接系统库?

转载 作者:可可西里 更新时间:2023-11-01 18:38:56 36 4
gpt4 key购买 nike

如何在大多数静态模式 (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
...

最佳答案

在您的 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

为了安全起见

关于c++ - 如何在 Bazel 中静态链接系统库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43416410/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com