Dynamically linking against libc++ doesn't work in NDK r14b

2,136 views
Skip to first unread message

Andreas Falkenhahn

unread,
Mar 26, 2018, 8:01:00 AM3/26/18
to andro...@googlegroups.com
I'm compiling a shared object using NDK r14b which uses C++11 and I'm linking dynamically against libc++. However, when dlopen()ing this shared object, I get the following error message:

dlopen failed: library "libc++_shared.so" not found

When I statically link against libc++ it's working fine. Isn't dynamically linking against libc++ supported?

--
Best regards,
Andreas Falkenhahn mailto:and...@falkenhahn.com

Ryan Prichard

unread,
Mar 26, 2018, 4:37:17 PM3/26/18
to andro...@googlegroups.com
Isn't dynamically linking against libc++ supported?

Yes, that is supported.

What version of Android do you see this problem with?

There's a known issue affecting versions older than Android 4.3, documented online:

Shared runtimes

If your app targets a version of Android earlier than Android 4.3 (Android API level 18), and you use the shared library variant of a given C++ runtime, you must load the shared library before any other library that depends on it.
 
Rather than managing this yourself, we recommend using ReLinker.


This problem can be fixed by calling loadLibrary() on libc++_shared.so first, then calling it on your app's shared library.

-Ryan


--
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+unsubscribe@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/1808181614.20180326140054%40falkenhahn.com.
For more options, visit https://groups.google.com/d/optout.

Andreas Falkenhahn

unread,
Mar 26, 2018, 6:07:30 PM3/26/18
to 'Ryan Prichard' via android-ndk
On 26.03.2018 at 22:37 'Ryan Prichard' via android-ndk wrote:

> Yes, that is supported.
> What version of Android do you see this problem with?
> There's a known issue affecting versions older than Android 4.3, documented online:
> Shared runtimes
> If your app targets a version of Android earlier than Android 4.3
> (Android API level 18), and you use the shared library variant of a
> given C++ runtime, you must load the shared library before any other library that depends on it.
>
> Rather than managing this yourself, we recommend using ReLinker.
> https://developer.android.com/ndk/guides/cpp-support.html#shared_runtimes
> This problem can be fixed by calling loadLibrary() on
> libc++_shared.so first, then calling it on your app's shared library.

Weird, I get the error on Android O. But since it works when linking
statically it's not that much of a problem. Still weird, though.

Ryan Prichard

unread,
Mar 26, 2018, 7:01:58 PM3/26/18
to andro...@googlegroups.com
The NDK's libc++_shared.so needs to be bundled in an APK's lib/<abi> directories, alongside other shared libraries. Maybe you're not including it?

-Ryan


--
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+unsubscribe@googlegroups.com.
To post to this group, send email to andro...@googlegroups.com.
Visit this group at https://groups.google.com/group/android-ndk.

Andreas Falkenhahn

unread,
Mar 27, 2018, 5:38:52 AM3/27/18
to 'Ryan Prichard' via android-ndk
On 27.03.2018 at 01:01 'Ryan Prichard' via android-ndk wrote:

> The NDK's libc++_shared.so needs to be bundled in an APK's
> lib/<abi> directories, alongside other shared libraries. Maybe you're not including it?

Yes, that was the problem. I didn't know that I had to include
the shared lib in the APK. I thought it would use a one
provided by the system. Works now, thanks.

Dan Albert

unread,
Mar 27, 2018, 2:44:29 PM3/27/18
to android-ndk
I thought it would use a one provided by the system.

That's the case for most NDK libraries, but in the case of the C++ stdlib it works out for the best to bundle it with the app. You wouldn't want to be locked out of C++17 just because you're supporting devices from 2016 :)


--
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.

Alexis Barra

unread,
Sep 6, 2018, 7:22:12 PM9/6/18
to android-ndk
Hey Dan,

I don't understand why this library works in a different way. Could you explain it? or maybe point me a resource to read to understand the reason.

Thanks!
Alexis

Ray Donnelly

unread,
Sep 7, 2018, 3:48:09 AM9/7/18
to andro...@googlegroups.com
You do not want there end users device dictating what c++ language specification you can use in your application so you should bundle the ndk DSO with your app. That way the ndk dictates this aspect.

Reply all
Reply to author
Forward
0 new messages