歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android通過C代碼實現動態和靜態鏈接

Android通過C代碼實現動態和靜態鏈接

日期:2017/3/1 9:46:17   编辑:Linux編程

Android中靜態鏈接C文件:

0,首先安裝gcc相關的編譯器,具體不詳述,筆者用的是mingw,也可以用其他的編譯器,網絡上很多安裝詳解,本文不做敘述。

MinGW 編譯FFmpeg 1.2.1 的H.264 decoder http://www.linuxidc.com/Linux/2014-01/95848.htm

1,創建C代碼,命名為hello,編寫內容:

#include<stdio.h>

int main(){

printf("hello c!!!");

return 0;

}

2,進入c文件指定的目錄中比如:d:\test中,通過gcc hello.c 或者gcc hello.c -static -o hellostatic命令生成可執行的exe文件。

注意:如果有no new line end the file的警告,在你編程的末尾敲一個回車就可以

3,啟動模擬器,通過adb命令(首先adb命令必須配置好,否則會有不支持命令錯誤),adb shell -->> cd data -->> mkdir c創建c文件夾

4,進入c文件指定的目錄中比如:d:\test中,執行D:\test>adb push hellostatic.exe /data/c,將文件載入到c文件中,

5,通過adb shell 命令,進入到c文件中,如:adb shell-->>cd data-->> cd c -->ls 執行chmod 777 hellostatic.exe 如# chmod 777 hellostatic.exe (設置權限)

6,繼續通過 ./hellostatic.exe命令執行程序,將會出現;'hello c!!!‘

總結:對於靜態鏈接生成的可執行文件比較大,不需要依賴任何系統庫,一切都是自包含的。

動態鏈接:

1,同上執行命令:D:\test>gcc -c Hello.c -o hello.o

因為使用的是動態鏈接,所以鏈接時候必須用到ANDROID下Linux的libraries。此類庫位於/system/lib 目錄下,

2, 進入system文件夾下,執行ls列出文件,其中有個是lib的目錄,進入並將其內容列出。各種so文件,同樣也有hw等文件夾,為了能正確的鏈接程序,我們將此目錄(system/lib)中的所有庫文件復制到本地文件夾中,通過adb pull命令,如C:\Users\Administrator>adb pull /system/lib d:\LIB\android_lib

3

4,通過上面的圖片執行後,接下來的步驟如靜態鏈接相似,執行命令(安裝)adb push->(控制權限)adb shell chmod 777->(運行)adb shell /data/c/hello.exe

更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved