android上進行c/C++開發測試)

來源:互聯網
上載者:User
Android C編程技巧

運行模擬器

  emulator -console

  * 將檔案寫入到模擬器的userdata.img檔案中

  adb push

  *將一個目錄拷貝到模擬器中,包括子目錄

  adb push

  * 將一個目錄從模擬器中拷出來

  adb pull

  * 使得模擬器可以運行arm代碼.

  使用GNU/ARM Linux編譯器編譯你的應用程式就可以了

  * 在模擬器裡面運行shell,需要先運行模擬器

  adb shell

  *運行模擬器中的一個控制台程式

  adb shell

  *串連模擬器的控制台

  telnet localhost 5554/6/8

  運行C程式

  參考文獻

  Native C "Hello World" working in emulator

  http://groups.google.com/group/a ... wse_thread/threa...

  Native C Applications for Android

  http://benno.id.au/blog/2007/11/13/android-native-apps

  步驟

  * 下載GNU/ARM編譯工具

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

  * 編寫c/c++代碼.

  * 使用GNU/ARM Linux 工具建立一個應用程式,不使用動態連結程式庫

ex. arm-none-linux-gnueabi-g++.exe -static -o hello HelloAndroid.cpp

  * 啟動模擬器

$SDK_ROOT/tools/emulator.exe

  * 在命令列視窗運行 abd將編譯好的hello程式放入模擬器的磁碟

 adb push hello /system/sbin/hello

  * 讓hello檔案變成可執行程式,不要使用 chmod ugo+x

 adb shell chmod 777 /system/sbin/hello

  * 運行hello程式

  adb shell

  cd /system/sbin/

  hello

  EXAMPLE HELLO WORLD CODE

  //
  // HelloAndroid.cpp
  //
  //

  #include
  using std::cin;
  using std::cout;
  using std::endl;
  class MyName
  {
  public:
  void getname( void );
  void sayhello( void );
  private:
  char name[ 255 ];
  };
  void MyName::getname( void )
  {
  cout << "What is your name? ";
  cin >> name;
  }
  void MyName::sayhello( void )
  {
  cout << "Welcome " << name << " to the world of Android" << endl;
  }
  MyName name;
  int main( int argc, char *argv[] )
  {
  name.getname();
  name.sayhello();
  return 0;
  }

Android編譯本地C++程式方法

在Android平台上程式以Java形式運行在Dalvik模擬器上,但Android作為一個Linux核心系統完全可以執行Navtive C++程式,主要的步驟如下:

  1.下載ARM C++交叉編譯器http://www.codesourcery.com/gnu_toolchains/arm/portal/subscription3057

  2.編寫本地C++代碼,如Hello Wolrd,可以使用標準庫STL。編譯的命令列如下

 arm-none-linux-gnueabi-g++.exe -static -oandroid123 android123.cpp

  首先運行arm-none-linux-gnueabi-g++.exe程式-static 參數代表靜態庫,-o為輸出名稱android123,最後的android123.cpp為原始碼。

  3.運行模擬器,用cmd在sdkTools目錄夏之星 adb pushandroid123 /system/sbin/android123

  4.設定存取權限,通過Linux的Shell,在cmd下設定所有使用者完全控制許可權adb shell chmod 777 /system/sbin/android123

  5.執行這個android123程式,輸入adb shell cd /system/sbin/android123即可

在android平台上測試C/C++程式及庫

int main( int argc, char *argv[] )
  {
  name.getname();
  name.sayhello();
  return 0;

  android平台上帶有標準C庫,我們可以寫個C程式來試試看能不能在上面運行。。。

  首先下載並安裝交叉編譯工具GNU/ARM Linux gcc:

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

  安裝時 直接解壓就行了,要設定好PATH環境變數。

  簡單的C代碼:

  test.c
  #include <stdio.h>
  int main()
  {
  int i,j;
  for(i=0;i<=10;i++)
  {
  for(j=0;j<=i;j++)
  printf(”*”);
  printf(”n”);
  }
   return 0;
  }

  用剛下載的交叉編譯工具編譯原始碼:

  # arm-none-linux-gnueabi-gcc test.c -o test -static

  -static選項在這裡是必須的,不然android平台就不運行此程式。

  這也說明了此平台上的C/C++庫是不能被C/C++程式動態串連的 。

  進入tools目錄,用adb工具下載到android平台,放到/data/data目錄。

  # ./adb push test /data/data

  進入/data/data目錄運行程式。

  # cd /data/data

  # ./test

  *

  **

  ***

  ****

  *****

  ******

  *******

  ********

  *********

  **********

  ***********

  ok,It’s done !

  C++程式一樣的方法,只不過編譯器換成:arm-none-linux-gnueabi-g++

  附C++範例程式碼:

  //
  // HelloAndroid.cpp
  //
  //
  #include <iostream>
  using std::cin;
  using std::cout;
  using std::endl;
  class MyName
  {
  public:
  void getname( void );
  void sayhello( void );
  private:
  char name[ 255 ];
  };
  void MyName::getname( void )
  {
  cout << “What is your name? “;
  cin >> name;
  }
  void MyName::sayhello( void )
  {
  cout << “Welcome “ << name << ” to the world of Android” << endl;
  }
  MyName name;
  }

  上面的應用程式在編譯時間必須加上-static選項,也就是在編譯時間將函數都靜態編譯到程式中了,運行時不用再動態串連。如果不加此選項,在android平台上就不讓運行。

  經過測試,將自己寫的庫放到/system/lib目錄下,然後寫個主程式來動態串連,也是無法運行。

  看來此平台做了限制,不讓C/C++的程式運行時動態串連到這些C/C++庫。

轉自:http://blog.chinaunix.net/u3/90973/showart_1958940.html

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.