android2.2下編譯c模塊

android2.2下編譯c模塊(原文出處)

Android SDK相當強大和全面了,但有時你的應用程序可能需要更多的功能,需要在android的linux基礎層面上運行應用程序。今天研究瞭如何在android環境下編譯c模塊,提供以下2種方法:

一、使用Android.mk自動編譯:

1、在$(yourAndroid)/development目錄下,創建一個hello目錄。

         #mkdir $(yourAndroid)/development/hello

       其中$(yourandroid)指android的源代碼目錄;

2、在hello目錄中,編寫hello.c,內容如下:

         #include <stdio.h> 
         int main()
          {
               printf("hello world/n");
               exit(0);

               //return 0;                       

           }
3、在hello目錄中,編寫Android.mk, 內容如下:

       LOCAL_PATH:= $(call my-dir)
       include $(CLEAR_VARS)
       LOCAL_MODULE := helloworld
       LOCAL_SRC_FILES := hello.c
        include $(BUILD_EXECUTABLE)

LOCAL_SRC_FILES指定源文件,LOCAL_MODULE指定要編譯的模塊名,include $(BUILD_EXECUTABLE)指定編譯成可執行文件,改爲BUILD_SHARED_LIBRARY爲動態鏈接庫,這些可參考$(yourAndroid)/build/core/config.mk

4、回到Android源代碼頂層目錄,進行編譯,cd $(your_andoird) && make helloworld

5、#adb push helloworld /data

二、手動編譯:

1、編譯成目標文件:

    #$(yourAndroid)/prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/arm-eabi-gcc -I bionic/libc/arch-arm/include/ -I bionic/libc/include -I bionic/libc/kernel/common -I bionic/libc/kernel/arch-arm -c helloworld.c  -o hello.o

2、生成可執行代碼:

    #$(yourAndroid)/prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/arm-eabi-gcc -nostdlib -Bdynamic -Wl,-T,build/core/armelf.x -Wl,-dynamic-linker,/system/bin/linker -Wl,--gc-sections -Wl,-z,nocopyreloc -o helloworld -Lout/target/product/generic/obj/lib -Wl,-rpath-link=out/target/product/generic/obj/lib -lc hello.o -entry=main

3、上傳文件:

    #adb push helloworld /data

編譯好之後,可用file和readelf查看可執行文件。

   # file helloworld

   helloworld:  ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), not stripped

  #readelf -d helloworld

  Dynamic section at offset 0x1000 contains 12 entries:
  Tag        Type                         Name/Value
 0x00000001 (NEEDED)                     Shared library: [libc.so]
 0x00000004 (HASH)                       0x80e8
 0x00000005 (STRTAB)                     0x8214
 0x00000006 (SYMTAB)                     0x8134
 0x0000000a (STRSZ)                      118 (bytes)
 0x0000000b (SYMENT)                     16 (bytes)
 0x00000015 (DEBUG)                      0x0
 0x00000003 (PLTGOT)                     0x9088
 0x00000002 (PLTRELSZ)                   16 (bytes)
 0x00000014 (PLTREL)                     REL
 0x00000017 (JMPREL)                     0x828c
 0x00000000 (NULL)                       0x0

這是ARM格式的動態鏈接可執行文件,運行時需要libc.so和libm.so。"not stripped"表示它還沒被STRIP。嵌入式系統中爲節省空間通常將編譯完成的可執行文件或動態庫進行STRIP,即去掉其中多餘的符號表信息。在前面"make helloworld showcommands"命令的最後我們也可以看到,Android編譯環境中使用了out/host/linux-x86/bin/soslim工具進行STRIP。

備註:在adb shell 終端下運行  ./helloworld如果出現

                                     [1] + Stopped (signal)        ./helloworld,

源文件中用exit(0) 替代return 0即可,出現的原因是這個位置的return調用造成棧堆下溢。

參考文摘:

1、http://blog.csdn.net/evilcode/archive/2010/07/14/5733390.aspx
2、http://wenku.baidu.com/view/76c618d5360cba1aa811dabd.html

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

****************************************************************************************************************

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 


發佈了27 篇原創文章 · 獲贊 6 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章