Native C++ "Hello World" working in emulator

来源:百度文库 编辑:神马文学网 时间:2024/05/03 10:04:50

Native C++ "Hello World" working in emulator

by adsouza » Mon Nov 19, 2007 11:45 am

Android OS Tips

* to start emulator
./emulator -console

* to xfer a file to emulator; this is stored in emulator's userdata.img file
adb push

* to copy a file or a directory recursively to emulator
adb push

* to copy a file or a directory recursively from emulator
adb pull

* emulator can run native ARM Linux code.
build your apps using GNU/ARM Linux toolchain and then run in emulator.

* to get a shell on the emulator
adb shell

* to run a console app on Android emulator
adb shell

* to connect to emulator console for specific commands
telnet localhost 5554/6/8

Building and Running a native C++ App on the Android Emulator

References

Native C "Hello World" working in emulator
http://groups.google.com/group/android-dev...87cc

Native C Applications for Android
http://benno.id.au/blog/2007/11/13/android-native-apps

Steps
* download and install GNU/ARM Linux tool chain
http://www.codesourcery.com/gnu_toolcha ... nload.html

* create C/C++ code. see below for sample code.

* build app without dynamic libraries using GNU/ARM Linux toolchain
ex. arm-none-linux-gnueabi-g++.exe -static -o hello HelloAndroid.cpp

* start emulator in Windows by double clicking on $SDK_ROOT/tools/emulator.exe

* in a Windows Command window, use "adb" to xfer executable to emulator disk
adb push hello /system/sbin/hello

* make your app executable; do not use chmod ugo+x
adb shell chmod 777 /system/sbin/hello

* run your app in a console on the emulator
adb shell
cd /system/sbin/
hello

EXAMPLE HELLO WORLD C++ CODE
Syntax: [ Download ] [ Hide ] [ Select ] [ Expand ]Syntax: [ Download ] [ Show ]Using c Syntax Highlighting
  1. EXAMPLE HELLO WORLD C++ CODE
  2. //
  3. // HelloAndroid.cpp
  4. //
  5. //
  6.  
  7. #include
  8. using std::cin;
  9. using std::cout;
  10. using std::endl;
  11.  
  12. class MyName
  13. {
  14.   public:
  15.     void getname( void );
  16.     void sayhello( void );
  17.  
  18.   private:
  19.     char name[ 255 ];
  20. };
  21.  
  22. void MyName::getname( void )
  23. {
  24.   cout << "What is your name? ";
  25.   cin >> name;
  26. }
  27.  
  28. void MyName::sayhello( void )
  29. {
  30.   cout << "Welcome " << name << " to the world of Android" << endl;
  31. }
  32.  
  33. MyName name;
  34.  
  35. int main( int argc, char *argv[] )
  36. {
  37.   name.getname();
  38.   name.sayhello();
  39.   return 0;
  40. }
Parsed in 0.017 seconds, using GeSHi 1.0.8.4
adsouza
Freshman
 
Posts: 6
Joined: Fri Nov 16, 2007 6:44 am
Location: Pune, India
  • Website
  • YIM