首先下载eclipse和cdt,我的版本号依次是:Version: Indigo Service Release 2和Version: 1.0.0.201202111925,再下载windows的ndk,我使用的是android-ndk-r9d

什么cygwin这等东西,太恶心了,下载慢,大的要命!复杂,今天给一个最爽的编译教程。

前面的cdt插件怎么这里pass,网上教程很多的。直接配置。。。

启动eclipse,然后点Windows-Prefrences-C/C++-Build-Envionment,添加以下路径

最新首发Eclipse+CDT+android-ndk写纯c++安卓应用(附openGL Es)_Andorid

然后创建一个android工程,把代码全部删除,资源全部删除,AndroidManifest.xml内容如下

 

[plain]  view plain copy
 
  1. APP_PLATFORM := android-14  

main.cpp

 

 

  
  •             ASensorEventQueue_setEventRate(engine->sensorEventQueue,  
  •                     engine->accelerometerSensor, (1000L / 60) * 1000);  
  •         }  
  •         break;  
  •     case APP_CMD_LOST_FOCUS:  
  •         // When our app loses focus, we stop monitoring the accelerometer.  
  •         // This is to avoid consuming battery while not being used.  
  •         if (engine->accelerometerSensor != NULL) {  
  •             ASensorEventQueue_disableSensor(engine->sensorEventQueue,  
  •                     engine->accelerometerSensor);  
  •         }  
  •         // Also stop animating.  
  •         engine->animating = 0;  
  •         engine_draw_frame(engine);  
  •         break;  
  •     }  
  • }  
  •   
  • /** 
  •  * This is the main entry point of a native application that is using 
  •  * android_native_app_glue.  It runs in its own thread, with its own 
  •  * event loop for receiving input events and doing other things. 
  •  */  
  • void android_main(struct android_app* state) {  
  •     struct engine engine = {0};  
  •     // Make sure glue isn't stripped.  
  •     app_dummy();  
  •     state->userData = &engine;  
  •     state->onAppCmd = engine_handle_cmd;  
  •     state->onInputEvent = engine_handle_input;  
  •      = state;  
  •   
  •     // Prepare to monitor accelerometer  
  •     engine.sensorManager = ASensorManager_getInstance();  
  •     engine.accelerometerSensor = ASensorManager_getDefaultSensor(  
  •             engine.sensorManager, ASENSOR_TYPE_ACCELEROMETER);  
  •     engine.sensorEventQueue = ASensorManager_createEventQueue(  
  •             engine.sensorManager, state->looper, LOOPER_ID_USER, NULL, NULL);  
  •   
  •     if (state->savedState != NULL) {  
  •         // We are starting with a previous saved state; restore from it.  
  •         engine.state = *(struct saved_state*) state->savedState;  
  •     }  
  •   
  •     // loop waiting for stuff to do.  
  •   
  •     while (true) {  
  •         // Read all pending events.  
  •         int ident;  
  •         int events;  
  •         struct android_poll_source* source;  
  •   
  •         // If not animating, we will block forever waiting for events.  
  •         // If animating, we loop until all events are read, then continue  
  •         // to draw the next frame of animation.  
  •         while ((ident = ALooper_pollAll(engine.animating ? 0 : -1, NULL,  
  •                 &events, (void**) &source)) >= 0) {  
  •   
  •             // Process this event.  
  •             if (source != NULL) {  
  •                 source->process(state, source);  
  •             }  
  •   
  •             // If a sensor has data, process it now.  
  •             if (ident == LOOPER_ID_USER) {  
  •                 if (engine.accelerometerSensor != NULL) {  
  •                     ASensorEvent event;  
  •                     while (ASensorEventQueue_getEvents(engine.sensorEventQueue,  
  •                             &event, 1) > 0) {  
  •                         LOGI("accelerometer: x=%f y=%f z=%f", event.acceleration.x, event.acceleration.y, event.acceleration.z);  
  •                     }  
  •                 }  
  •             }  
  •   
  •             // Check if we are exiting.  
  •             if (state->destroyRequested != 0) {  
  •                 engine_term_display(&engine);  
  •                 return;  
  •             }  
  •         }  
  •   
  •         if (engine.animating) {  
  •             // Done with events; draw next animation frame.  
  •             engine.state.angle += .01f;  
  •             if (engine.state.angle > 1) {  
  •                 engine.state.angle = 0;  
  •             }  
  •             engine_draw_frame(&engine);  
  •         }  
  •     }  
  • }  

创建完成收工,然后创建另外一个工程。路径必须是刚才创建工程的jni目录,名字随便,重点看图

 

最新首发Eclipse+CDT+android-ndk写纯c++安卓应用(附openGL Es)_Andorid_02

好了点完成,然后打开main.cpp发现N多错误,直接下设置一下环境变量,右键工程,属性(是刚创建的C++工程)

最新首发Eclipse+CDT+android-ndk写纯c++安卓应用(附openGL Es)_Andorid_03

接下来看图,把所有的库加进去。

最新首发Eclipse+CDT+android-ndk写纯c++安卓应用(附openGL Es)_Andorid_04

 

最后加一个Symbol,其实就是定义一个宏,告诉编译器我现在的平台是Android,add

最新首发Eclipse+CDT+android-ndk写纯c++安卓应用(附openGL Es)_Andorid_05

最后点OK,所有的函数都能正常识别,提示功能也可以用了。开发效率高多了。编译直接点锤子就行了。

最新首发Eclipse+CDT+android-ndk写纯c++安卓应用(附openGL Es)_Andorid_06

然后在原来的工程运行安装就行了!