2017-04-14 65 views
3

如何在靜態模式(linkstatic = 1)下靜態鏈接系統庫?我嘗試使用「-Wl,-Bstatic -lboost_thread -Wl,-Bdynamic」或「-Wl,-Bstatic」,「-lboost_thread」,「-Wl,-Bdynamic」,但它們都不起作用。我不想在系統中硬編碼libboost_thread.a的路徑。如何在Bazel中靜態鏈接系統庫?

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 
... 

回答

0

在工作區文件中定義的外部存儲庫...

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

在cc_binary規則創建boost_thread.BUILD文件

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

然後加入

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

並投入

linkstatic = 1 

爲了安全起見。

+0

什麼是工作區文件? –

+1

boost_thread.BUILD應該位於何處? – tweej