Compiling shared libraries

212 views
Skip to first unread message

Muhui Jiang

unread,
Nov 22, 2018, 9:07:08 AM11/22/18
to android-ndk
Hi

I am using the ndk-build to build the native shared libraries.

My sample code only consists 8 easy functions.  However, I find that the target libtest.so consists many other functions that are related to ndk according to their names. 

For example, std::__ndk1::char_traits<char>::length(const std::__ndk1::char_traits<char>::char_type *__s)。 I saw many functions with the prefix ndk_xxx. I was wondering whether these function has to be here. Is it possible to generate a libtest.so that only contains the function in my sample code and package all the other functions that are required by ndk in the other shared libraries?  Many Thanks

Regards
Muhui

Alex Cohn

unread,
Nov 24, 2018, 4:49:42 AM11/24/18
to android-ndk
Apparently, your code uses STL. The default is static linking of STL runtime, but you can try ANDROID_STL=c++_shared. This will generate a smaller libtest.so, but you will need to pack the libc++_shared.so (which is prebiuilt in NDK) into your APK.

BR,
Alex

Dan Albert

unread,
Nov 26, 2018, 3:10:27 PM11/26/18
to andro...@googlegroups.com
It looks like your application contains multiple shared libraries using the STL. If that's the case, you need to do as Alex says and switch to the shared STL (or no STL). See our docs: https://developer.android.com/ndk/guides/cpp-support#static_runtimes

If all you want to do is guarantee that only your API is exported from your library, use a version script or `-fvisibility=hidden` couple with `__attribute__((visibility("default")))` to control the exported API surface.

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.
To post to this group, send email to andro...@googlegroups.com.
Visit this group at https://groups.google.com/group/android-ndk.
To view this discussion on the web visit https://groups.google.com/d/msgid/android-ndk/ee020924-1b50-49cf-9ae4-7f677b10c5b5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

AppCoder

unread,
Nov 27, 2018, 11:43:14 AM11/27/18
to android-ndk
Does the visibility hidden let you mix and match shared libs/STL/exception handling and run all the statics in parallel?

Alex Cohn

unread,
Dec 2, 2018, 2:39:32 PM12/2/18
to android-ndk
On Tuesday, November 27, 2018 at 6:43:14 PM UTC+2, AppCoder wrote:
Does the visibility hidden let you mix and match shared libs/STL/exception handling and run all the statics in parallel?

Mixing STL runtimes is a dangerous and tricky subject, this can be done correctly, but it's usually easier and safer to switch to single shared STL runtime. At any rate, this has nothing to do with visibilityI"hidden"). The most common problem of multiple STL runtimes is when new allocates from one heap, and delete operates on another heap. No visibility of API can help to protect against misuse of this pattern.
Reply all
Reply to author
Forward
0 new messages