I’m trying to creating a singleton for 2 different libraries in android ndk.
Imagine that I have:
- lib1 (static)
- lib2 (shared)
- lib3 (shared)
lib1 is always defined as LOCAL_STATIC_LIBRARIES of lib2 and lib3
But when I set the value of the singleton on lib2, this value remains NULL on lib3.
How can I build a singleton with this architecture?
The problem is common and admits no good solution, as discussed here and here. The shared libraries have separate copies of linked-in static libraries, both code and data. The static library instances in different SO’s just don’t interact, and for many good reasons, too.
What you need is a chunk of memory that is accessible to both. You can move the singleton up to the Java level (it’s under Android after all). You can have a temporary file with a well known name. C-level TLS might help, too.
Android’s shared memory facility (
ashmem) will be of no use, as it does not provide named blocks.