使用Bazel构建C++项目并链接共享库

5
我将基于bazel tutorial for 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",
    ],
)

但这不像是最好的解决方案。是否有更好的方法来创建和链接一个共享库?
1个回答

3
如果在二进制文件中指定linkstatic标志,它将链接所有库作为静态库或共享库。但我不知道如何仅将某些库链接为共享库。
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,
)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接