4

I'm new to android programming, I was reading the answers on this question Why shouldn't an Android app be written in C/C++ because you "simply prefer to program in C/C++"? in the first answer by Devunwired. he mentioned that "your native code must be built into .so files (one for armv5, armv7 and x86) all packaged up into the same APK. This duplication of executable code makes your app 3x the size (i.e. a "fat binary") unless you take on the task of building separate APKs for each architecture when you distribute the application"

My question is, I always thought that Operating Systems in general provide an abstraction over the underlying architecture. So if I want to deploy a c/c++ program on different environments, I need to re-compile the source code using a compiler written for different operating systems, this operating system however might have different versions that support different architecture. If I'm right then why the case is different when it comes to Android?

Community
  • 1
  • 1
  • 2
    An OS does not provide an abstraction over the machine's instruction set. (Well, normally anyway. The performance penalty is prohibitive.) – Alan Stokes Apr 16 '16 at 19:08

1 Answers1

4

At some point the operating system hands the code over to the CPU. The binary must have instructions for that particular CPU. With Java each OS and CPU makes the same virtual machine available so you don't have the same considerations.

wally
  • 10,717
  • 5
  • 39
  • 72
  • 1
    you said it! the OS hands the code to the CPU! the code doesn't access the CPU directly, thus the OS add a level of abstraction over CPU! – Newbie And Curious Apr 16 '16 at 19:03
  • 2
    If there is no virtual machine then the code does access the CPU directly. Very directly. The 1s and 0s in the binary are literally the ones loaded into the registers, decoded etc. Some instructions are privileged, but that doesn't mean the rest are handled by the OS first. – wally Apr 16 '16 at 19:06
  • the 1s and 0s always endup being loaded into register even if there is a virtual machine! the difference is that in case of a virtual machine this can only happen after JIT! – Newbie And Curious Apr 16 '16 at 19:09
  • 1
    C and C++ almost universally target the actual machine, not a virtual machine. – Alan Stokes Apr 16 '16 at 19:11
  • does this mean that C compilers are targeted towards the machine architecture and not the OS? to elaborate, how does the C compiler translate printf to native code? does it access the monitor directly? or does the OS delegate the functionality ? – Newbie And Curious Apr 16 '16 at 19:14
  • 2
    @NewbieAndCurious Have a look at this: http://stackoverflow.com/questions/2135788/what-do-c-and-assembler-actually-compile-to – Daniel Nugent Apr 16 '16 at 19:22