

This is what we call “cross-compilation”.

This is tricky because by default, compilers use the same CPU architecture and the same libc used by the operating system they run on 2. Make sure we can tell the compiler about the CPU architecture.So if want to achieve our goal (running the “Hello, world” C program on a simulator and on a phone), we have to do two things:
ANDROID NDK CANNOT LINK FUNCTION ANDROID
Android uses yet an other implementation called bionic. There are other implementations like musl for instance. On your laptop you are probably using glibc. There are several implementations of “the libc”. We call such a library “the libc”, which is a bit misleading. See the line about libc.so.6 ? That’s the library that contains the code for the printf function we just used. Those files do not exist on Android, because even if Android is based on Linux, it’s still a different operating system. so files: $ export LD_TRACE_LOADED_OBJECTS=1 Well, if we re-run the binary we just built we can see it loads a few. Remember when we used LD_TRACE_LOADED_OBJECTS back in a previous post?
ANDROID NDK CANNOT LINK FUNCTION HOW TO
The file does exist, but Android does not know how to run it. If we try the same thing with a x86_64 simulator, we get an even weirder error: $ adb push hello /data/local/tmp OK, but the Android SDK also comes with a simulator, and we can choose the CPU architecture.Ĭan’t we just use a x86_64 Android simulator ? This means that a binary you built for a x86_64 CPU will not run on a armv7 CPU. The trick is that different CPU have different instructions sets.įor instance, it is very likely that the CPU you have on your laptop is a x86_64 CPU, and that the CPU of you phone is an armv7.

You can think of this binary file as a list of instructions, ready to be used by a CPU. When you use a compiler, you get a binary file from the source code. (Note that /data/local/tmp is the only directory I found where we can run executables): $ adb push hello /data/local/tmp And now, let’s use adb push to copy the binary on our phone, and adb shell to try and run it:
