18
votes

Is Android POSIX-compatible? I know it uses the Linux kernel, but I'm not sure if that means it's POSIX compatible, as the POSIX standard deals more with userland functions. So, is it compatible?

For example, if I use only ANSI and POSIX functions in my C program, would it compile and run on Android without needing any code-changes?

4
Even Linux isn't fully POSIX compatible :)Shmil The Cat
@ShmilTheCat I thought it's just not certified because it costs money?sashoalm
IMO its more than cost issues, see personal.opengroup.org/~ajosey/tr28-07-2003.txtShmil The Cat
another difference from a POSIX system is that /tmp isn't guarantee writable or existingOld Badman Grey

4 Answers

21
votes

GNU libc (glibc) is too big and complicated for mobile phones, so Android implements its own special version of libc which is Bionic libc, which itself does not fully support POSIX. One of the most lacking features in the android Bionic libc is pthread_cancel(), so if you don't use it, your code will probably do fine.

And also as @code monkey mentioned you can take a look to the bionic source code. You can find additional information here .

6
votes

Android is not fully POSIX compatible. First what I can notice is default c library. As you may know it is called glibc. But Android has its own c library - bionic. Here are some notes.

Some functions within Bionic's POSIX and system call headers are stubs or wrappers for Android-specific behavior, causing unintended behavior in some instances.

Android uses linux-based kernel, so you can say that it is POSIX compliant. But in general Android doesn't comply with Unix specifications such as POSIX either. Also you can read something like that

Bionic does not include C++ exception handling, perhaps as Google does not use C++ exceptions and Java exceptions are available once the Java virtual machine is started

Bionic does not include the Standard Template Library, and developers must include it manually if they need it

There is great custom build NDK - CrystaX NDK

Key features of the CrystaX NDK:

Wide characters. Google's NDK doesn't support wide chars properly in C or C++. With the CrystaX NDK, you get full standard compliant wide characters support. You can easily port existing code that uses wide characters/strings/streams or write new code.

The most recent toolchains The CrystaX NDK includes the most recent versions of GCC and Clang compilers as well as stables. This allows developers to use new language abilities (such as new C++ 11 features). All compilers are built with high- and low-level optimizations which enables generation of the most efficient code for target hardware.

C++11 support Since the CrystaX NDK includes the most recent versions of GCC and Clang, it supports many new C++ 11 features listed on C++0x/C++11 Support in GCC and C++98 and C++11 Support in Clang. In addition, the CrystaX NDK offers fully working C++ 11 classes std::thread, std::mutex, std::chrono etc. These classes are not available in the Google NDK because of lack of functionality in Android Bionic libc. We have investigated this problem and fixed it, so in the CrystaX NDK you can just use them and forget about ifdefs.

Objective-C support The only languages the Google NDK supports are C and C++. The CrystaX NDK adds support of Objective-C in addition to C and C++. Only the core language is supported as of now; work on Cocoa-like libraries is in progress. To start using Objective-C in your project, just add source files with the extension .m (Objective-C) or .mm (Objective-C++) and specify them in LOCAL_SRC_FILES in Android.mk.

To be continued... If you don't see some great feature here, don't hesitate to contact us and ask for it. You can also use our issue/bug tracker to report bugs or feature requests. And, of course, contributions are welcome!

You can find more information on Official CrystaX NDK site

4
votes

I know this answer is bit outdated, but it would supplement answers above.

Yes, Android is not POSIX-compatible, mainly because of it's libc (Bionic) restrictions. However, using CrystaX NDK you may not feel that difference so hard - just because using CrystaX NDK development for Android become much more POSIX-compatible. We've implemented many libc parts (buggy or absent in Bionic) on our own in libcrystax.so, core of CrystaX NDK, and did it without altering of typical development flow. We're going to improve it further, since libcrystax don't yet support full POSIX set, but as of now, it supports many things such as wide characters and string, full support for locales (locale-specific input and output), full math library including complex and type-generic functions, fully working backend for Standard C++ Library (two implementations available, on your choice - GNU libstdc++ or LLVM libc++) and tons of other fixes and improvements.

Just to show how CrystaX NDK makes native development for Android easier - we've included Boost 1.57.0 into CrystaX NDK 10.1.0, which we have built from it's sources without any modifications - just because in CrystaX NDK Boost stay on top of much more POSIX-conformant base than in Google's NDK.

3
votes

Official Bionic in tree documentation quote

https://android.googlesource.com/platform/bionic/+/37ad9597839c70a7ec79578e5072df9c189fc830/docs/status.md

Run ./libc/tools/check-symbols-glibc.py in bionic/ for the current list of POSIX functions implemented by glibc but not by bionic. Currently (2017-10):

aio_cancel
aio_error
aio_fsync
aio_read
aio_return
aio_suspend
aio_write
lio_listio
pthread_cancel
pthread_mutex_consistent
pthread_mutex_getprioceiling
pthread_mutex_setprioceiling
pthread_mutexattr_getprioceiling
pthread_mutexattr_getprotocol
pthread_mutexattr_getrobust
pthread_mutexattr_setprioceiling
pthread_mutexattr_setprotocol
pthread_mutexattr_setrobust
pthread_setcancelstate
pthread_setcanceltype
pthread_testcancel
wordexp
wordfree
libm

Current libm symbols: https://android.googlesource.com/platform/bionic/+/master/libm/libm.map.txt

0 remaining missing POSIX libm functions.

Bionic Wikipedia page

https://en.wikipedia.org/wiki/Bionic_(software)#Differences_from_POSIX

Also has some interesting info:

Although bionic aims to implement all of C11 and POSIX, there are still (as of Oreo) about 70 POSIX functions missing[8] from libc. There are also POSIX functions such as the endpwent/getpwent/setpwent family that are inapplicable to Android because it lacks a passwd database. As of Oreo, libm is complete.

Some functions deliberately do not conform to the POSIX or C standards for security reasons, such as printf which does not support the %n format string.[9]