The current helloworld. c source file is as follows:
# Include <stdio. h>
Int main (){
Printf ("Hello world! \ N ");
}
How to compile and execute it on Android? This is the goal of this article.
Principle
(Skip this section if you only want to understand how to do this .)
Anyone familiar with Android Application Development knows that Java is used for Android!
Some may deny: "No, Android NDK can be developed using C/C ++ and other native code ."
Correct. However, the native library file compiled by NDK, as the library form, still needs to be called by Java code through JNI.
Some may say: "NDK provides methods that only write native code but not Java code ."
Correct. However, you will find that this method still requires editing some xml files by yourself. In fact, there is still an Activity executed on the Java Virtual Machine for calling.
What we want to achieve is: Like in Linux, use the following sentence:
$ Gcc helloworld. c-o helloworld
You can compile a helloworld that can be run directly, and then execute:
$./Helloworld
The output is as follows:
$ Hello world!
So how can we achieve this goal? First, we need to clarify some theoretical knowledge:
1. Android is a Linux-based operating system, so you can regard it as a Linux (I don't know if this sentence is too absolute. If there is an error, I hope to correct it );
2. if the program is not executed on the virtual machine, but in the Linux operating system, therefore, this program must be compiled by a compiler named "specific hardware platform for the Linux operating system. For example, gcc in our general release version is compiled for your PC, and this executable program can also be used on the same hardware platform. But if it is placed on an embedded platform similar to arm, it is obviously not executable (because arm and your pc have different instruction sets ). If you want to use the same source code to compile programs that can be run on the arm, you need to use the arm Compiler (such as linux-arm-gcc) for compilation. This is called cross-compilation. Those who have studied embedded development must understand.
3. What is the nature of NDK? If you use the editor to open ndk-build, you will be surprised to find that it is not a binary code, but a shell script, and is very simple. At last, you will call the local make. Ndk-build: Explains jni/Android. the mk File Syntax converts it to a format similar to "linux-arm-gcc xxx. c-shared-o-Ixx-Lxx libxxx. so ". Therefore, the nature of ndk I understand is similar to make, and the Android. mk of Makefile is interpreted. Unfortunately, the packaging made by NDK allows us to compile only lib (it has a connection option ).
4. Since ndk-build is only a make rather than a compiler, the real compiler must also be in the NDK package. We can use these cross-compilation tool chains for compilation.
5. let's look at the question again. "NDK compiles the executable program on the Android character interface." What we need to do is not compile it with NDK, but compile it with the cross-compiled toolchain in NDK, the compiled program is not running on the "Android character interface". Specifically, it runs on the "Linux" on the "Hardware executed by 'andorid.
Method
See Standalone Toolchain in Android NDK Dev guide.pdf (documentation.html in the ndk package or google.
Here is a simple description of the process:
1. Identify the tool chain of cross-compilation. Run the following command:
SYSROOT = $ NDK/platforms/android-<level>/arch-<arch>/
$ NDK indicates the path of the NDK installation, level indicates the Android version, and arch indicates the hardware structure. All depends on your own situation. For example:
SYSROOT = $ NDK/platforms/android-8/arch-arm
2. Set the compiler and enter the following command:
Export CC = "$ NDK/toolchains/<name>/prebuilt/<system>/bin/<prefix> gcc -- sysroot = $ SYSROOT"
All depends on your own situation. For example:
Export CC = "$ NDK/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/arm-linux-androideabi-gcc -- sysroot = $ SYSROOT"
3. After the environment is configured, run the following command:
$ CC helloworld. c-o helloworld
You can get a helloworld that can be executed on the "Hardware executed by 'andorid.
Test
Open the Android virtual machine or connect to the Development Board
Use adb push to upload helloworld to Android;
Use adb shell to enter the Android shell;
Find the just-passed helloworld and execute #./helloworld to see the output!
# Hello world!
From Peking University-Google Android lab