歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android - 動態庫雙向依賴解決方法

Android - 動態庫雙向依賴解決方法

日期:2017/3/1 10:19:29   编辑:Linux編程

問題:

昨天調試一個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. /* 由link的庫實現 */
  4. extern void testA();
  5. extern void testB();
  6. /* 由本身庫實現而由外部調用 */
  7. extern void testC();
  8. extern void testD();
  9. struct AAInterface{
  10. void (*testA)();
  11. void (*testB)();
  12. };
  13. extern void setInterface(struct AAInterface *cb);
  14. #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. static struct AAInterface g_aa_interface ;
  7. /* 由link的庫實現 */
  8. extern void testA(){
  9. g_aa_interface.testA();
  10. }
  11. extern void testB(){
  12. g_aa_interface.testB();
  13. }
  14. extern void testCall(){
  15. LOGI("testCall 111");
  16. testA();
  17. LOGI("testCall 222");
  18. testB();
  19. LOGI("testCall 333");
  20. }
  21. /* 由本身庫實現而由外部調用 */
  22. extern void testC(){
  23. LOGI("testC call in--->");
  24. testCall();
  25. LOGI("testC call out<---");
  26. }
  27. extern void testD(){
  28. LOGI("testD call in--->");
  29. testCall();
  30. LOGI("testD call out<---");
  31. }
  32. extern void setInterface(struct AAInterface *cb){
  33. LOGI("setInterface call in -->");
  34. memset((void*)&g_aa_interface,0x00,sizeof(g_aa_interface));
  35. g_aa_interface.testA = cb->testA;
  36. g_aa_interface.testB = cb->testB;
  37. LOGI("setInterface call out <--");
  38. }
Copyright © Linux教程網 All Rights Reserved