簡體   English   中英

玩 Bazel C++ 教程; build 不創建/使用共享庫?

[英]Playing with Bazel C++ tutorials; build does not create/use shared libraries?

今天第一次看到巴澤爾。

在構建cpp 教程時,我可以看到它如何構建簡單的可執行文件和存檔庫,但它看起來不像示例創建或使用共享庫。

有誰知道簡單的示例 BUILD 文件來演示這個過程? 謝謝。

共享庫是cc_binary

cc_binary(
    name = "libfoo.so",
    srcs = ["foo.cc"],
    linkshared = 1,     ## important
)

(在非平凡的情況下,您可能還應該添加linkstatic = 1以獲得一個獨立的 DSO,它本身在其源依賴項上沒有加載時依賴項。)

為了執行動態鏈接,您必須首先導入共享庫。 您應該指定庫頭、庫二進制文件和接口庫(僅 Windows 需要,本示例中不存在):

# Build the shared library
cc_binary(
    name = "libfoo.so",
    srcs = ["foo.cc"],
    linkshared = 1,     ## important
)

# Import the shared library
cc_import(
    name = "imported_libfoo",
    hdrs = ["foo.h"],
    shared_library = "libfoo.so",
)

# Link to the shared library
cc_binary(
    name = "bar",
    deps = [":imported_libfoo"],
)

基於上面的方法,我加一點例外。

以上面的示例為例,如果您的庫也依賴於另一個cc_library ,請添加alwayslink = Truecc_library ,否則共享庫將沒有符號鏈接。

cc_library(
    name = "hello-greet-lib",
    srcs = ["hello-greet.cc"],
    hdrs = ["hello-greet.h"],
    alwayslink = True,   # important
)

cc_binary(
    name = "libhello-greet.so",  # the shared library
    linkshared = True,
    deps = [":hello-greet-lib"],
)

cc_import(
    name = "hello-greet",
    shared_library = "libhello-greet.so",
    hdrs = ["hello-greet.h"],
)

cc_binary(
    name = "hello-world",
    srcs = ["hello-world.cc"],
    deps = [
        ":hello-greet",
        "//lib:hello-time",
    ],
)

這在您需要聚合一些庫時很常見,所以如果您想以上述方式生成動態庫,請記住添加alwayslink = True

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM