Android ndk solves the problem of mutual calls between the two so

Source: Internet
Author: User

From: http://blog.csdn.net/victoryckl/article/details/6832333

Modules A and B are mainly implemented in C and need to be compiled into two. so, B. so, but. so calls B. so function, B. so also called. so functions, and for some reason, A and B must be compiled separately. The problem is that no matter which module is compiled first, the compilation fails because they are mutually dependent.

This dependency must be canceled during compilation. In the following program, use dlopen to open so and dlsym to obtain the function pointer to avoid this dependency.

A. c

 

[CPP]View plaincopyprint?
  1. # Include <stdio. h>
  2. # Include <stdlib. h>
  3. # Include <stdarg. h>
  4. # Include <dlfcn. h>
  5. # Include <JNI. h>
  6. Char * getstringa (void)
  7. {
  8. Return "I am in A. So ";
  9. }
  10. Jstring java_com_ckl_socallso_socallsoactivity_fucntionina (jnienv * ENV, jobject thiz)
  11. {
  12. Return (* env)-> newstringutf (ENV, getstringa ());
  13. }
  14. Jstring java_com_ckl_socallso_socallsoactivity_acallb (jnienv * ENV, jobject thiz)
  15. {
  16. Jstring ret;
  17. // So path:/data/package name of my program/lib/My so file name
  18. Void * filehandle = dlopen ("/data/COM. CKL. socallso/lib/libb. So", rtld_lazy );
  19. If (filehandle)
  20. {
  21. Char * (* funcptrb) (void) = NULL;
  22. Funcptrb = dlsym (filehandle, "getstringb ");
  23. If (funcptrb)
  24. {
  25. Ret = (* env)-> newstringutf (ENV, funcptrb ());
  26. }
  27. Else
  28. {
  29. Ret = (* env)-> newstringutf (ENV, "dlsym getstringb failed! ");
  30. }
  31. Dlclose (filehandle );
  32. }
  33. Else
  34. {
  35. Ret = (* env)-> newstringutf (ENV, "dlopen failed! ");
  36. }
  37. Return ret;
  38. }

# Include <stdio. h> <br/> # include <stdlib. h> <br/> # include <stdarg. h> <br/> # include <dlfcn. h> <br/> # include <JNI. h> </P> <p> char * getstringa (void) <br/>{< br/> return "I am in. so "; <br/>}</P> <p> jstring java_com_ckl_socallso_socallsoactivity_fucntionina (jnienv * ENV, jobject thiz) <br/>{< br/> return (* env) -> newstringutf (ENV, getstringa (); <br/>}</P> <p> jstring java_com_ckl_socallso_socallsoactivity_ac Allb (jnienv * ENV, jobject thiz) <br/>{< br/> jstring ret; <br/> // so path: /data/package name of my program/lib/My so file name <br/> void * filehandle = dlopen ("/data/COM. CKL. socallso/lib/libb. so ", rtld_lazy); <br/> If (filehandle) <br/>{< br/> char * (* funcptrb) (void) = NULL; <br/> funcptrb = dlsym (filehandle, "getstringb"); <br/> If (funcptrb) <br/>{< br/> ret = (* env) -> newstringutf (ENV, funcptrb (); <br/>}< br/> else <br />{< Br/> ret = (* env)-> newstringutf (ENV, "dlsym getstringb failed! "); <Br/>}< br/> dlclose (filehandle ); <br/>}< br/> else <br/> {<br/> ret = (* env)-> newstringutf (ENV, "dlopen failed! "); <Br/>}</P> <p> return ret; <br/>}< br/>
B .C

 

 

[CPP]View plaincopyprint?
  1. # Include <stdio. h>
  2. # Include <stdlib. h>
  3. # Include <stdarg. h>
  4. # Include <dlfcn. h>
  5. # Include <JNI. h>
  6. Char * getstringb (void)
  7. {
  8. Return "I am in B. So ";
  9. }
  10. Jstring java_com_ckl_socallso_socallsoactivity_fucntioninb (jnienv * ENV, jobject thiz)
  11. {
  12. Return (* env)-> newstringutf (ENV, getstringb ());
  13. }
  14. Jstring java_com_ckl_socallso_socallsoactivity_bcalla (jnienv * ENV, jobject thiz)
  15. {
  16. Jstring ret;
  17. // So path:/data/package name of my program/lib/My so file name
  18. Void * filehandle = dlopen ("/data/COM. CKL. socallso/lib/LIBA. So", rtld_lazy );
  19. If (filehandle)
  20. {
  21. Char * (* funcptra) (void) = NULL;
  22. Funcptra = dlsym (filehandle, "getstringa ");
  23. If (funcptra)
  24. {
  25. Ret = (* env)-> newstringutf (ENV, funcptra ());
  26. }
  27. Else
  28. {
  29. Ret = (* env)-> newstringutf (ENV, "dlsym getstringa failed! ");
  30. }
  31. Dlclose (filehandle );
  32. }
  33. Else
  34. {
  35. Ret = (* env)-> newstringutf (ENV, "dlopen failed! ");
  36. }
  37. Return ret;
  38. }

# Include <stdio. h> <br/> # include <stdlib. h> <br/> # include <stdarg. h> <br/> # include <dlfcn. h> <br/> # include <JNI. h> </P> <p> char * getstringb (void) <br/>{< br/> return "I am in B. so "; <br/>}</P> <p> jstring java_com_ckl_socallso_socallsoactivity_fucntioninb (jnienv * ENV, jobject thiz) <br/>{< br/> return (* env) -> newstringutf (ENV, getstringb (); <br/>}</P> <p> jstring java_com_ckl_socallso_socallsoactivity_bc Alla (jnienv * ENV, jobject thiz) <br/>{< br/> jstring ret; <br/> // so path: /data/package name of my program/lib/My so file name <br/> void * filehandle = dlopen ("/data/COM. CKL. socallso/lib/LIBA. so ", rtld_lazy); <br/> If (filehandle) <br/>{< br/> char * (* funcptra) (void) = NULL; <br/> funcptra = dlsym (filehandle, "getstringa"); <br/> If (funcptra) <br/>{< br/> ret = (* env) -> newstringutf (ENV, funcptra (); <br/>}< br/> else <br />{< Br/> ret = (* env)-> newstringutf (ENV, "dlsym getstringa failed! "); <Br/>}< br/> dlclose (filehandle ); <br/>}< br/> else <br/> {<br/> ret = (* env)-> newstringutf (ENV, "dlopen failed! "); <Br/>}< br/> return ret; <br/>}
Android. mk

 

 

[CPP]View plaincopyprint?
  1. Local_path: = $ (call my-DIR)
  2. Include $ (clear_vars)
  3. Local_module: =
  4. Local_src_files: = A. C
  5. Include $ (build_shared_library)
  6. Include $ (clear_vars)
  7. Local_module: = B
  8. Local_src_files: = B. C
  9. Include $ (build_shared_library)

Local_path: = $ (call my-DIR) </P> <p> include $ (clear_vars) <br/> local_module: = A <br/> local_src_files: =. c <br/> include $ (build_shared_library) </P> <p> include $ (clear_vars) <br/> local_module: = B <br/> local_src_files: = B. c <br/> include $ (build_shared_library)

 

A. c. B. c generate LIBA. so, libb. so, LIBA. so call libb. the getstringb () function in so, libb. so call LIBA. the getstringa () function in so.

[CPP]View plaincopyprint?
 
 

 

In addition, the path of the so file is/data/package name of my program/lib/My so file name.

Engineering source codeSocallso.7z

The running effect is as follows:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.