bazel c++ 创建并链接共享库

Chr*_*aes 5 c++ shared-libraries bazel

我的问题将基于bazel c++ 教程的第二阶段

通常,此示例将创建与libhello-greet.a静态链接的hello-world。不过,我想创建与libhello-greet.so动态链接的hello-world

因此,我通过使用此BUILD文件找到了某种解决方法:

cc_binary(
    name = "libhello-greet.so",
    srcs = ["hello-greet.cc", "hello-greet.h"],
    linkshared = 1,
)

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

cc_binary(
    name = "hello-world",
    srcs = ["hello-world.cc"],
    deps = [
        ":libhello-greet",
    ],
)
Run Code Online (Sandbox Code Playgroud)

但这感觉不是最好的解决方案。有没有更好的方法来创建和链接共享库?

Mik*_*yke 3

如果您linkstatic在二进制文件中指定 -flag,它将把所有库链接为静态库或共享库。但我不知道如何仅将某些库链接为共享库。

cc_library(
    name = "hello-greet",
    srcs = ["hello_greet.cc"],
    hdrs = ["hello_greet.h"],
)

cc_binary(
    name = "hello-world",
    srcs = ["main.cc"],
    deps = [
        ":hello-greet",
    ],
    linkstatic=False,
)
Run Code Online (Sandbox Code Playgroud)