iOS 從C移植項目到Objective-C,iosobjective-c
一、建立項目
iOS | Framework & Library
Cocoa Touch Static Library
建立一個Library庫
1. M.h標頭檔
#ifndef M_h#define M_h#include <stdio.h>
void testSleep(int t);void testPthread(int n);#endif /* M_h */
2. M.c實現檔案
#include "M.h"#include <stdio.h>#include <pthread.h>#include <unistd.h>#include <stdlib.h>void testSleep(int t){ printf("testSleep:\n"); int i; for (i=0; i<10; i++) { printf("sleep...\n"); usleep(t*1000); printf("return...\n"); }}void *thrFun(void *p){ int t = (int)p; int i; for (i = 0; i<5; i++) { printf("thrFun %d\n", t); sleep(1); } return NULL;}void testPthread(int n){ void *retval; pthread_t *tid = (pthread_t *)malloc(sizeof(pthread_t)*n); int i; for (i=0; i<n; i++) pthread_create(&tid[i], NULL, thrFun, (void *)i); for (i=0; i<n; i++) pthread_join(tid[i], &retval);}
cmd + B 編譯,此時只編譯模擬器了版本,可以串連手機編譯真機版本靜態庫檔案,編譯成功後會在電腦上產生相關的.a靜態庫檔案;
libM.a檔案所在目錄
/Users/xx/Library/Developer/Xcode/DerivedData/M-xxx/Build/Products/Debug-iphonesimulator/Users/xx/Library/Developer/Xcode/DerivedData/M-xxx/Build/Products/Debug-iphoneos
兩個路徑分別是模擬器,真機版本的輸出目錄。
將libM.a庫檔案和M.h標頭檔拷貝到外部項目即可使用靜態庫裡面的函數了。
二、查看 .a 檔案支援的平台
通過lipo命令來查看
lipo -info xxLibrary.a
輸出結果:
Architectures in the fat file: xxLibrary.a are: armv7 armv7s i386 x86_64 arm64
上面幾個平台分別對應的手機
- armv7是iphone5之前的裝置指令集架構;
- armv7s是iphone5、iphone 5s的指令集架構;
- arm64是iphone6、iphone 6plus的指令集架構;
- i386以及x86_64是MAC的指令集架構;
如果某個 .a 檔案只支援某一個平台,但我們的應用在發布的時候,都會需要支援所有平台的庫,所以build會報錯,此時可以把各個支援單個平台的 .a 檔案合并。
lipo -create XXXX_V7.a XXXX_V7s.a -output XXXX_all.a
三、查看 .framework 檔案支援的平台
lipo -info ./****.framework/****
輸出結果:
Architectures in the fat file: ./****.framework/**** are: i386 armv7 armv7s
如此便可查看你工程中的靜態庫是否支援64位。