[英]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 = True
到cc_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.