NDK 使用STL静态库注意,不要两个SO文件同时静态链接一个STL静态库

NDK 使用STL静态库注意,不要两个SO文件同时静态链接一个STL静态库,具体解释如下:

Static runtimes

Please keep in mind that the static library variant of a given C++ runtime SHALL ONLY BE LINKED INTO A SINGLE BINARY for optimal conditions.

What this means is that if your project consists of a single shared library, you can link against, e.g., stlport_static, and everything will work correctly.

On the other hand, if you have two shared libraries in your project (e.g. libfoo.so and libbar.so) which both link against the same static runtime, each one of them will include a copy of the runtime's code in its final binary image. This is problematic because certain global variables used/provided internally by the runtime are duplicated.

This is likely to result in code that doesn't work correctly, for example:

  • memory allocated in one library, and freed in the other would leak or even corrupt the heap.
  • exceptions raised in libfoo.so cannot be caught in libbar.so (and may simply crash the program).
  • the buffering of std::cout not working properly

This problem also happens if you want to link an executable and a shared library to the same static library.

In other words, if your project requires several shared library modules, then use the shared library variant of your C++ runtime.

参考链接:NDK Programmer's Guide

发布者

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注