Android ndk 中使用STL .

http://blog.csdn.net/ryfdizuo/article/details/9172881

 

ndk版本:android-ndk-r8d,基于ndk自带samples中的test-libstdc++工程进行修改。


1. 例子工程目录结构:

test-libstdc++ / jni / Android.mk

test-libstdc++ / jni / Application.mk

test-libstdc++ / jni / test-libstdc++.cpp


2. Android.mk文件:

  1. # A simple test for the minimal standard C++ library   
  2. #   
  3. LOCAL_PATH := $(call my-dir)  
  4.   
  5. include $(CLEAR_VARS)  
  6. LOCAL_MODULE := test-libstl  
  7. LOCAL_SRC_FILES := test-libstl.cpp  
  8. LOCAL_LDLIBS    := -llog  
  9.   
  10. include $(BUILD_EXECUTABLE)  
  11.   
  12. include $(CLEAR_VARS)  
  13. LOCAL_EXECUTABLE := test-libstl  
# A simple test for the minimal standard C++ library
#
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := test-libstl
LOCAL_SRC_FILES := test-libstl.cpp
LOCAL_LDLIBS    := -llog

include $(BUILD_EXECUTABLE)

include $(CLEAR_VARS)
LOCAL_EXECUTABLE := test-libstl


3. Application.mk内容:

  1. APP_STL := stlport_static  
APP_STL := stlport_static


4. test-libstdc++.cpp内容:

  1. #include <iostream>   
  2. #include <string>   
  3. #include <vector>   
  4. using namespace std;  
  5.   
  6. int main() {  
  7.     vector<string> contents;  
  8.     for (int i=0; i<5; i++)   
  9.     {  
  10.         contents.push_back(string("vector"));  
  11.     }  
  12.       
  13.     for (int i=0; i<5; i++)   
  14.     {  
  15.         cout << contents[i] << endl;  
  16.     }  
  17.       
  18.     cout << "hello, world\n";  
  19.     return 0;  
  20. }  
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
	vector<string> contents;
	for (int i=0; i<5; i++) 
	{
		contents.push_back(string("vector"));
	}
	
	for (int i=0; i<5; i++) 
	{
		cout << contents[i] << endl;
	}
	
	cout << "hello, world\n";
	return 0;
}


5. 最后在test-libstdc++\libs\armeabi目录下面生成test-libstl 可执行程序。原理类似tcpdump工具使用,通过adb推到手机上,在shell模式下运行改可执行程序:

  1. D:\android-sdk-windows\platform-tools>adb push test-libstl /data/local/  
  2. 1341 KB/s (251396 bytes in 0.182s)  
  3.   
  4. D:\android-sdk-windows\platform-tools>adb shell chmod 755 /data/local/test-libstl  
  5.   
  6. D:\android-sdk-windows\platform-tools>adb shell /data/local/test-libstl  
  7. vector  
  8. vector  
  9. vector  
  10. vector  
  11. vector  
  12. hello, world  
D:\android-sdk-windows\platform-tools>adb push test-libstl /data/local/
1341 KB/s (251396 bytes in 0.182s)

D:\android-sdk-windows\platform-tools>adb shell chmod 755 /data/local/test-libstl

D:\android-sdk-windows\platform-tools>adb shell /data/local/test-libstl
vector
vector
vector
vector
vector
hello, world


参考:http://tungchingkai.blogspot.com/2011/08/how-to-build-android-ndk-sample-using.html

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值