Compile C/C++ In Eclipse for Android


Software Version:

   Cygwin 1.7.15-1

   Eclipse IDE for C/C++ Developers(Juno)

   Eclipse Classic 4.2 (Juno)

   CDT 8.1.0 for Eclipse Juno

   Android ADT 14.0.0

   Android SDK 4.0.3 (API 15)

   Android NDK r8

 

A.Eclipse IDE for C/C++ Developers + Cygwin

Install Eclipse IDE for C/C++ Developers.

Install Cygwin.Important step!You had better download the full package and install from local.Installing from Internet always failed and may lost file!

  The first time I install it from the Internet ,but it occured a file failure and I ignore it.Then ,holy shit!!! I spend 2 days to debug the errors result from the file failure!!So remember that you had better install the full package and from the local.

               

click the "Install" after "All".

  Install/Download from Internet.If the warning “Download Incomplete,Try again? ” comes out or it hangs on a certain progress,you can cancel it and repeat the download steps.Remember two points:

  Ⅰ.Select the same download website with the last time.

  Ⅱ.Installing type,select ”Reinstall“ not ”install“.(may needn't)

  Then you can continue the download progress or you have to install from 0.

  

  I download it first for a night and then install it for a day.A whole day!

  Probably you have to set the envionment variable PATH。PATH should include x:\cygwin\bin,x:\cygwin is your cygwin install folder。

  My is f:\cygwin.Then PATH=f:\cygwin\bin. 

 Restart Eclipse,File->New->C++ Project

                

click Finish,Build it first and then run it.


B.Eclipse Classic Cygwin +CDT

Install Eclipse Classic 4.2.

Install CDT for Eclipse.

  Open the CDT download page,you will see the following content:

      pic B-1

                     <pic B-1>

  The p2 software repository is the address you should access in Eclipse.Help->Install New Software,click Add button and input the address:

          

  click OK,then you will see the next:

    

  click next to install them.

  However,it hanged at this progress for a long time:

          

  Then I canceled  it ,and clicked the cdt-master-8.1.0.zip of  pictrue <pic B-1>,wanting to download it first and install it from local directory.

    

  click Archive to select the local zip file.

  Finally,I installed the CDT successfully.

Install Cygwin.The same with Part A.

Restart Eclipse,File ->New -> Others,and you'll see C/C++ project.

 


C.Eclipse Classic + Cygwin + CDT + NDK -> so Lib

①②③ The same with part B①②③.

Install NDK.Download the zip file and unzip it to the destination folder.my is D:\Android\android-ndk-r8

  Edit file \cygwin\home\user_name\.bash_profile,if you can't find the file in this location,copy the cygwin\etc\skel\.bash_profile to here,add the following to the end: 

# Android NDK

  NDK=/cygdrive/<你的盘符>

  export NDK

My:

  ndk=/cygdrive/D/Android/android-ndk-r8

  export ndk

  ndk is a custom name ,you can change it.  

  Save and restart cygwin.Go to D:\Android\android-ndk-r8\samples\hello-jni\jni :

      cd $ndk/samples/hello-jni/jni

  

  execute command$ndk/ndk-build

  

 Pay attention to these content of google document:

 Required development tools

  • For all development platforms, GNU Make 3.81 or later is required. Earlier versions of GNU Make might work but have not been tested.
  • A recent version of awk (either GNU Awk or Nawk) is also required.
  • For Windows, Cygwin 1.7 or higher is required. The NDK will not work with Cygwin 1.5 installations.

 If you installed the wrong version,you may get the error of the following pictrue:

   

Create NDK Builder for Eclipse.

  Right click your project->Properties->Builders->New->Program:

  Main  location:the path of file bash.exe in cygwin/bin.

     working directory:the path of cygwin/bin.

      arguments:--login -c "cd /cygdrive/d/Workspace2/hello&&$ndk/ndk-build"

      /cygdrive/d/Workspace2/hello is the absolute path of your android project.

   if you can't input the argument or it can't be recognized,make sure that you have added the path of ndk (D:\Android\android-ndk-r8) to the environment PATH.

  

  In label Refresh,specific the folder needed to refresh when compilation end.Choose libs,create it if none.

  

  About the label Build Options,use to set the folder needed to monitor.Run the builder when the content of the folder changed.

  Specify working set... choose the folder of the c/c++ code.

  

  Completed! you can use the builder in your android project.

 


D.Show Chinese in Cygwin

  Right Click->Options:

  

  Set the character set to GBK,not UTF-8

E.A Project For Test

  ①Create a empty android project.

  ②Create jni folder to contain c/c++ code.

  ③Create libs folder to contain generated libs.

  ④Write your c/c++ code in jni.

   My:hello.cpp

int test() {
    return 5;
}

  ⑤Write your Android.mk file.you can copy it from the ndk sample project.

  ⑥Press "Ctrl+S" to save it and the lib will be generated automatically.

   

  ⑦Generate the lib which can be used in android.

    In order to use the lib in Android, you have to modify the C/C++ code to coordinate with android.

    

   If the package name or activity name not coordinate with c/c++ method name,UnsatisfiedLinkError may occurs.

   To use the lib:

    

原文地址:https://www.cnblogs.com/qiengo/p/2587754.html