Android - 动态库双向依赖解决方法


问题:

昨天调试一个CA库link失败的问题:ca厂商一般提供的都是静态ca库,这样子你直接将其与你的库link在一起即可使用,但由于apk在ndk中编译器:Android-ndk-r6b\arm-linux-androideabi-4.4.3

而ca库使用hisi编译器:arm-eabi-4.4.0_hisi 两者使用的编译不同,所以需要在linux android环境下将ca静态库打包成动态库,而且用户实现的ca函数将会link失败,生成的动态库将在ndk中使用。

下面是一个简单的测试例子,用于说明一下如何做到相互依赖而编译生成动态库的方法

1、首先编译生成动态库

首先定义头文件:test.h

  1. #ifndef XXX_TEST_H___  
  2. #define XXX_TEST_H___   
  3.   
  4. /* 由link的库实现 */  
  5. extern void testA();  
  6. extern void testB();  
  7.   
  8. /* 由本身库实现而由外部调用 */  
  9. extern void testC();  
  10. extern void testD();  
  11.   
  12. struct AAInterface{  
  13.     void (*testA)();  
  14.     void (*testB)();  
  15. };  
  16.   
  17. extern void setInterface(struct AAInterface *cb);  
  18.   
  19. #endif /* XXX_TEST_H___ */   

然后实现文件:testA.c

  1. #include <assert.h>  
  2. #include <stdlib.h>   
  3. #include <string.h>   
  4. #include <cutils/log.h>   
  5. #include "test.h"   
  6.   
  7. static struct AAInterface g_aa_interface ;  
  8.   
  9. /* 由link的库实现 */  
  10. extern void testA(){  
  11.     g_aa_interface.testA();  
  12. }  
  13.   
  14. extern void testB(){  
  15.     g_aa_interface.testB();  
  16. }  
  17.   
  18. extern void testCall(){  
  19.     LOGI("testCall 111");  
  20.     testA();  
  21.     LOGI("testCall 222");  
  22.     testB();  
  23.     LOGI("testCall 333");  
  24. }  
  25.   
  26. /* 由本身库实现而由外部调用 */  
  27. extern void testC(){  
  28.     LOGI("testC call in--->");  
  29.     testCall();  
  30.     LOGI("testC call out<---");  
  31. }  
  32.   
  33. extern void testD(){  
  34.     LOGI("testD call in--->");  
  35.     testCall();  
  36.     LOGI("testD call out<---");  
  37. }  
  38.   
  39. extern void setInterface(struct AAInterface *cb){  
  40.     LOGI("setInterface call in -->");  
  41.     memset((void*)&g_aa_interface,0x00,sizeof(g_aa_interface));  
  42.     g_aa_interface.testA = cb->testA;  
  43.     g_aa_interface.testB = cb->testB;  
  44.     LOGI("setInterface call out <--");  
  45. }  
  • 1
  • 2
  • 下一页

相关内容