我的问题将基于 bazel tutorial for c++ 的阶段 2 .
通常此示例将创建 hello-world 与 libhello-greet.a 链接静态。但是,我想创建 hello-world 与 libhello-greet.so 链接动态。
因此,我通过使用此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",
],
)
但这并不是最好的解决方案。有没有更好的方法来创建和链接共享库?
最佳答案
如果您指定 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,
)
关于c++ - bazel c++ 创建并链接共享库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53514630/