在 Android 平台上使用 Bazel 时,有时会遇到 c++_shared 和 c++_static 问题,具体表现为链接库出现错误,或无法链接到正确的库版本。
解决这个问题的方法是通过在 BUILD 文件中进行设置来明确指定链接库版本。例如,若要使用某个特定版本的 c++_shared 库,可以按照以下方式进行设置:
android_binary(
name = "my_binary",
srcs = ["my_binary.cc"],
deps = [
"//my_lib:my_library_shared",
],
linkopts = [
"-Wl,-rpath,$ORIGIN/../lib",
"-L$(execpath //my_lib:)",
"-lmy_library_shared"
],
)
在上述代码中,我们使用 linkopts 参数指定了链接选项,用来指向正确版本的 c++_shared 库。在这个例子中,$ORIGIN 表示当前可执行文件所在的目录,因此通过 -Wl ,我们指定了一个相对路径来指向 lib 目录中正确的 c++_shared 库。
类似地,对于 c++_static 库,可以通过如下方式进行配置:
android_binary(
name = "my_binary",
srcs = ["my_binary.cc"],
deps = [
"//my_lib:my_library_static",
],
linkopts = [
"-L$(execpath //my_lib:)",
"-lmy_library_static"
],
)
在这里,我们没有指定动态链接库路径,而是简单地将静态库链接到可执行文件中。
这样,通过明确指定链接库版本并为不同的库类型进行单独的设置,我们就可以解决 Android 平台上使用 Bazel 时出现的 c++_shared/c++_static 问题。