Android原生(Native)C開發之一 環境搭建篇

轉載:

轉載: Android原生(Native)C開發之一 環境搭建篇

Android是基於Linux的操做系統,處理器是ARM的,因此要在Linux或Windows等x86系統上編譯Android能運行的程序,你須要一個交叉編譯器。html

在Linux下面,你能夠自已編譯一個交叉編譯環境,但Windows下面,就比較複雜(也能夠在cygwin中編譯一個),但你能夠選擇下載一個現成的交叉編譯環境:linux

http://www.codesourcery.com/gnu_toolchains/arm/download.htmlandroid

Windows: http://www.codesourcery.com/gnu_toolchains/arm/portal/package3400/public/arm-none-linux-gnueabi/arm-2008q3-41-arm-none-linux-gnueabi.exeshell

Linux: http://www.codesourcery.com/gnu_toolchains/arm/portal/package3399/public/arm-none-linux-gnueabi/arm-2008q3-41-arm-none-linux-gnueabi.bin操作系統

安裝好了以後,將 CodeSourcery編譯器的bin目錄 (個人是D:\Program Files\CodeSourcery\Sourcery G++ Lite\bin)加入你的PATH環境變量中,就能夠開始你的Android Native C開發之旅了,寫好一個簡單的C程序:命令行

#includecode

int main(int argc, char** argv) {
printf(」hello android!\nI’m %s!\nI like android very much!!!\n」, 「Martin Foo」);htm

return 0;
}開發

另存成hello.c,進入命令行模式,確保交叉編譯器的bin目錄,及Android SDK的tools目錄在你的系統環境變量的path裏面,用以下命令編譯:編譯器

arm-none-linux-gnueabi-gcc -static hello.c -o hello

注意,必定要加上static參數,不然編譯好的可能會在Android上不能運行。

啓動Android模擬器,用以下命令將文件push到Android模擬器上:

adb shell mkdir /dev/sample
adb push hello /dev/sample/hello

adb shell chmod 777 /dev/sample/hello

先建立 /dev/sample目錄,再將編譯好的hello上傳上去,最後將hello改爲可執行的。

再進入命令行模式,進入Android的shell環境:

adb shell

#cd /dev/sample

#./hello

進入 /dev/sample目錄,執行hello,運行結果以下圖:

Android Native編譯環境

Android Native編譯環境

環境搭建篇

Android是基於Linux的操做系統,處理器是ARM的,因此要在Linux或Windows等x86系統上編譯Android能運行的程序,你須要一個交叉編譯器。

在Linux下面,你能夠自已編譯一個交叉編譯環境,但Windows下面,就比較複雜(也能夠在cygwin中編譯一個),但你能夠選擇下載一個現成的交叉編譯環境:

http://www.codesourcery.com/gnu_toolchains/arm/download.html

Windows: http://www.codesourcery.com/gnu_toolchains/arm/portal/package3400/public/arm-none-linux-gnueabi/arm-2008q3-41-arm-none-linux-gnueabi.exe

Linux: http://www.codesourcery.com/gnu_toolchains/arm/portal/package3399/public/arm-none-linux-gnueabi/arm-2008q3-41-arm-none-linux-gnueabi.bin

安裝好了以後,將 CodeSourcery編譯器的bin目錄 (個人是D:\Program Files\CodeSourcery\Sourcery G++ Lite\bin)加入你的PATH環境變量中,就能夠開始你的Android Native C開發之旅了,寫好一個簡單的C程序:

#include

int main(int argc, char** argv) {
printf(」hello android!\nI’m %s!\nI like android very much!!!\n」, 「Martin Foo」);

return 0;
}

另存成hello.c,進入命令行模式,確保交叉編譯器的bin目錄,及Android SDK的tools目錄在你的系統環境變量的path裏面,用以下命令編譯:

arm-none-linux-gnueabi-gcc -static hello.c -o hello

注意,必定要加上static參數,不然編譯好的可能會在Android上不能運行。

啓動Android模擬器,用以下命令將文件push到Android模擬器上:

adb shell mkdir /dev/sample
adb push hello /dev/sample/hello

adb shell chmod 777 /dev/sample/hello

先建立 /dev/sample目錄,再將編譯好的hello上傳上去,最後將hello改爲可執行的。

再進入命令行模式,進入Android的shell環境:

adb shell

#cd /dev/sample

#./hello

進入 /dev/sample目錄,執行hello,運行結果以下圖:

Android Native編譯環境

Android Native編譯環境

相關文章
相關標籤/搜索