Use Makefile to create a Dylib instance under the iPhone

Source: Internet
Author: User

PassMakefileInIPhoneCreateDylibThe instance is the content to be introduced in this article. First, I want to declare thatDylibIt cannot be escaped.IphoneOn c. I wrote this article to improve the previous article.

1. Create dylibtest. c and. h.

A test function is written here.

Dylibtest. h

Void test ();
 
Dylibtest. c

 
 
  1. #include "dylibtest.h"  
  2. #include "stdio.h"  
  3. void test() {  
  4.     printf("this is a test\n");  
  5. }  
  6.  
  7. makefile   
  8.  
  9. CC=/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/gcc  
  10. CFLAGS= -arch armv6 -isysroot /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.2.sdk  
  11. CPP=/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/cpp  
  12. target:  
  13.  $(CC) $(CFLAGS) -dynamiclib -o sotest-iphone.dylib dylibtest.c 

Here we use sdk4.2 arch as armv6, and we also need to remind you that if you compile the. m file and use the framework, you can write it like this during compilation.

 
 
  1. -framework Foundation 

The following is the test code testdylib.

The key code is as follows:

 
 
  1. NSString *path = [[NSBundle mainBundle] pathForResource:@"sotest-iphone" ofType:@"dylib"];  
  2. void* handle = dlopen([path cStringUsingEncoding:NSUTF8StringEncoding], RTLD_LAZY);  
  3. if (!handle) {  
  4.     printf("%s\n", dlerror());  
  5.     return;  
  6. }  
  7. void (*test)();  
  8. test = (void (*)())dlsym(handle, "test");  
  9. const char *dlsym_error = dlerror();  
  10. if (dlsym_error) {  
  11.     printf("%s\n", dlsym_error);  
  12.     dlclose(handle);  
  13.     return;  
  14. }  
  15. // use it to do the calculation  
  16. test();      
  17. // close the library  
  18. dlclose(handle); 

You can test it, but I believe the result will not be satisfactory to you.

Conclusion: PassMakefileInIPhoneCreateDylibI hope this article will help you with the introduction of the instance!

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.