4

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?

7
  • 1
    It probably comes with the compiler. Like Xcode for iOS.
    – Bo Persson
    Jan 2, 2018 at 21:19
  • 2
    Those libraries come with NDK C/C++ toolchains, and most likely are stuffed into the resulting .apk. Several implementations of C++ standard library are included, from which you can choose. Jan 2, 2018 at 21:20
  • 3
    c and c++ library are different thing. C runtime library is a part of any Linux distribution, so android has to have it. I imagine, iOS is the same thing, since it is Unixy in nature. C++ is a different thing, and probably has to be a separate library inside .apk.
    – SergeyA
    Jan 2, 2018 at 21:37
  • 2
  • @SergeyA You seemed confused. The CRT and C library are two different things. You have different C libraries, like glibc, uClibc, and newlib which are platform independent. The CRT is platform-dependent. This is going to be a combination of the kernel and files provided by the compiler (see crt0.o, et al.) It is not Linux/Unix specific by any means, nor does it imply that Androis/iOS are forced to use the same CRT/C library. Jan 2, 2018 at 22:17

1 Answer 1

7

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.