Build a normal C program on Android via GCC

Source: Internet
Author: User

1. Compiling executable programs 1.1 compiled with MK scriptDirectory structure:
Mk_appjnimain.cAndroid.mk
Android.mk content is very simple:
Local_path: = $ (call My-dir) include $ (clear_vars) local_module:= main_execlocal_src_files: = Main.cinclude $ (BUILD_ Executable)
command, CD to the top level of the JNI directory, and then execute the ndk-build command:
E:\github\ndk_tutorial\android_build_c_exec\mk_ap>ndk-build[armeabi] Compile thumb  : main_exec <= main.c[ Armeabi] Executable     : Main_exec[armeabi] Install        : main_exec = libs/armeabi/main_exec
Under the JNI sibling directory, the executable program is generated under libs/armeabi/main_exec
1.2 compiled with the Arm-linux-androideabi-gcc.exe tool provided by the Android NDKDirectory structure:
Gcc_appmain.c
command line, CD to the Gcc_app directory, and then execute the following command:
D:\android-ndk-r9b-windows-x86\toolchains\arm-linux-androideabi-4.6\prebuilt\windows\bin\ Arm-linux-androideabi-gcc.exe--sysroot=d:\android-ndk-r9b-windows-x86\platforms\android-13\arch-arm-o main_exec Main.c
Generate the Main_exec executable in the current directory.
--sysroot is the include and Lib directories of the execution system, removing the error that will be reported in a bunch of definitions. Specifically, GCC--help can be viewed.
2.ADB Copy the main_exec to the phone to perform the following:
E:\GitHub\ NDK_TUTORIAL\ANDROID_BUILD_C_EXEC\GCC_APP>ADB push main_exec/sdcard/1528 kb/s (6260 bytes in 0.004s) E:\GitHub\ndk_ TUTORIAL\ANDROID_BUILD_C_EXEC\GCC_APP>ADB shell[email protected]:/$ CP/SDCARD/MAIN_EXEC/DATA/LOCAL/CP/ SDCARD/MAIN_EXEC/DATA/LOCAL/CP:/data/local/main_exec:permission denied1| [email protected]:/$ cp/sdcard/main_exec/data/local/tmpcp/sdcard/main_exec/data/local/tmp[email  protected]:/$ cd/data/local/cd/data/local/[email protected]:/data/local $ cd TMPCD tmp[email protected]:/ Data/local/tmp $ ls-l main_execls-l main_exec-rwxr-x--x Shell Shell 6260 2014-06-06 16:30 main_exec[email  ;p rotected]:/data/local/tmp $ chmod 751 main_execchmod 751 main_exec[email protected]:/data/local/tmp $./main_ Exec./main_exechello World 
Via ADB push to sdcard, then into shell mode, copy from SDcard to/data/local/tmp (note that my N4 phone has insufficient permissions on/data/local, go to tmp). chmod 751 Sets the executable permissions, then executes.!.
3. Refer to 1. Http://stackoverflow.com/questions/21424366/how-to-use-arm-linux-androideabi-compiler2. Http://stackoverflow.com/questions/9868309/how-can-i-run-c-binary-executable-file-in-android-from-android-shell
Related code: HTTPS://GITHUB.COM/DIZUO/NDK_TUTORIAL/TREE/MASTER/ANDROID_BUILD_C_EXEC

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.