Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is the C/C++ Standard Library in Android and iOS?

Tags:

c++

c

android

ios

As far as I know one can can write apps on Android and iOS in C/C++, instead of Java and Objective-C.

Since both C and C++ rely upon their Standard Libraries, where and how the C/C++ Standard Library is implemented in such OSes? Is it part of something bigger like e.g. the Android NDK? If it isn't, can I use a different implementation if needed?

like image 321
Ignorant Avatar asked Mar 07 '23 05:03

Ignorant


1 Answers

There are several concepts at play here, I'm going to try to keep it brief. Android uses Bionic as its C library. It also lists different C++ libraries, however, they recommend you stick with libc++ (Clang) since they have stopped supporting libstdc++ (what they call gnustl) and STLPort is ancient. Now even though they call libstdc++ the system runtime, for libstdc++ in particular, the support library is called actually libsupc++. In order to have exception and RTTI support, you need to implement/build this, which doesn't seem to be the case for Android.

For Apple, it's a different story. XCode is the IDE (not the compiler!) On old versions of Mac, they shipped an ancient version of GCC. There was a transition period where they used llvm-gcc and symlinked gcc to clang. Now the latest version of XCode only supports LLVM/Clang. By default, Mac uses libc++, but you can select libstdc++ if you prefer. Keep in mind that although Clang tries to be as ABI-compatible with GCC as possible, it's probably not wise to mix libraries compiled by libc++/libstdc++.

Can you use a different C library in your toolchain? Not easily. You would need to port the C library of choice (i.e, Newlib) to the platform which is not trivial. Further, you would need to build a cross compiler toolchain that not only targets that system but also uses the new library. You will have to look into people who have already done this for you.

Now even if it was easy, there really isn't a good reason to. More often than not you only want to swap out selective parts of the library, like malloc. Android in particular can use jemalloc for example.

like image 144
user9163035 Avatar answered Mar 16 '23 00:03

user9163035