How do I link the system library statically in Bazel?
How to link the system library statically in main static mode (linkstatic = 1)? I tried using "-Wl, -Bstatic -lboost_thread -Wl, -Bdynamic" or "-Wl, -Bstatic", "-lboost_thread", "-Wl, -Bdynamic" but none of them worked. I don't want to hardcode the libboost_thread.a path on the system.
cc_binary(
name = "main",
srcs = [
"main.cpp",
],
linkopts = [
"-lboost_thread",
],
)
And the boost_thread library is linked as a dynamic library.
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
...
+3
source to share
1 answer
In the WORKSPACE file define an external repository ...
new_local_repository(
name = "boost_thread",
path = "/usr/lib/x86_64-linux-gnu",
build_file = "boost_thread.BUILD"
)
Create file boost_thread.BUILD
cc_library(
name = "lib",
srcs = ["libboost_thread.a"],
visibility = ["//visibility:public"],
)
Then in your cc_binary rule add
deps = ["@boost_thread//:lib",],
and enter
linkstatic = 1
to be safe.
0
source to share