通過Makefile在iPhone下建立Dylib執行個體

來源:互聯網
上載者:User

通過MakefileiPhone下建立Dylib執行個體是本文要介紹的內容,首先我要聲明下經過測試,第三方的dylib是無法在未越獄的iphone上c成功啟動並執行。寫這篇文章也只是為了完善之前的那篇文章。

1、建立dylibtest.c 和.h

這裡隨便寫了個test函數。

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 

這裡用的是sdk4.2 arch為armv6,另外需要提醒的事,如果你編譯的是.m檔案使用到framework的話編譯時間可以這樣寫

 
  1. -framework Foundation 

下面是測試代碼 testdylib

關鍵代碼如下:

 
  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); 

你可以去測試下了,不過相信結果應該不會很讓你滿意。

小結:通過MakefileiPhone下建立Dylib執行個體的內容介紹完了,希望本文對你有所協助!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.