使用 Android NDK 构建时隐藏来自 gnustl_static 的符号

Hiding symbols from gnustl_static when build with Android NDK

我正在构建一个 Android 库,其中包含一个使用 STL 编写的本机部分,该部分隐藏在 Java 包装器下。在 Static runtimes 部分的 NDK 文档中指出我应该使用共享运行时来避免多个静态运行时出现问题,但我想尝试使用 gnustl_static 并设置标志 -fvisibility=hidden。 问题是,它会解决可能的问题还是共享库是唯一的出路?

不,使用 -fvisibility=hidden 构建将无助于解决将多个库链接到静态 C++ 运行时所产生的问题。

除非您自己构建 gnustl_static,否则 -fvisibility=hidden 标志仅适用于依赖于 gnustl_static 的本机代码。它不会改变符号从 gnustl_static 导出的方式。

In addition, if you link two shared libraries–or a shared library and an executable– against the same static runtime, the final binary image of each shared library includes a copy of the runtime's code. Having multiple instances of runtime code is problematic because of duplication of certain global variables that the runtime uses or provides internally.

但是,只要您只创建一个共享库,针对静态 C++ 运行时的链接应该工作得很好。

This problem does not apply to a project comprising a single shared library. For example, you can link against stlport_static, and expect your app to behave correctly. If your project requires several shared library modules, we recommend that you use the shared library variant of your C++ runtime.