Android上通過gcc編譯普通的C程式

來源:互聯網
上載者:User

標籤:android   compiler   android ndk   shell   

1.編譯可執行程式 1.1 通過mk指令碼編譯目錄結構:
mk_appjnimain.cAndroid.mk
Android.mk內容十分滿簡單:
LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE:= main_execLOCAL_SRC_FILES := main.cinclude $(BUILD_EXECUTABLE)
命令下,cd到jni的上一級目錄,然後執行ndk-build命令:
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
在jni同級目錄下, libs/armeabi/下產生可執行程式main_exec
1.2 通過android ndk提供的arm-linux-androideabi-gcc.exe工具編譯目錄結構:
gcc_appmain.c
命令列,cd到gcc_app目錄下,然後執行下面命令:
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
在目前的目錄下產生main_exec可執行程式。
--sysroot是執行系統的include和lib目錄,去掉會報一堆定義找不到錯誤。具體可以gcc --help查看。
2.adb將main_exec拷貝到手機上執行具體操作如下:
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 protected]:/data/local/tmp $ chmod 751 main_execchmod 751 main_exec[email protected]:/data/local/tmp $ ./main_exec./main_exechello world
通過adb push到sdcard上,然後進入shell模式,從sdcard複製到/data/local/tmp 下(注意我的n4手機上/data/local下許可權不夠,進入tmp可以)。chmod 751 設定可執行程式許可權,然後執行。!。
3.參考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
相關代碼:https://github.com/dizuo/ndk_tutorial/tree/master/android_build_c_exec

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.