ios 製做framework

原文:http://db-in.com/blog/2011/07/universal-framework-iphone-ios-2-0/ios

 

原文廢話太多啊,本身總結一下,由於我是在原有的靜態庫工程基礎上創建的,因此新建一個target就行了。c++

1 新建target,macOS中的bundle:xcode

2 接下來設置bundle的build setting:(直接複製了)app

  • Base SDK: Latest iOS (iOS X.X) (in the X.X will appear the number of the lastest iOS SDK installed on your machine).
  • Architectures: $(ARCHS_STANDARD_32_BIT) armv6 (it’s very important to be exactly this value including the space before 「armv6″) This setting is valid to Xcode 4.2, if you are using an old version, use the 「Standard (armv6 armv7)」 option. (the values for this property depend on the value of the item bellow, so set that first).
  • Build Active Architecture Only: NO (otherwise we can’t compile to armv6 and armv7 at the same time).
  • Valid Architecture: $(ARCHS_STANDARD_32_BIT) (it’s very important to be exactly this value). If your Xcode is showing two lines with armv6 and armv7, delete then and insert this value in one single line.
  • Dead Code Stripping: NO.
  • Link With Standard Libraries: NO.
  • Mach-O Type: Relocatable Object File. This is the most important change. Here, we instruct the compiler to treat the Bundle as a relocatable file, by doing this, we can turn it into a framework with the wrapper setting.
  • Other Linker Flags: This setting is not mandatory, but if you are planning to use any kind of C++ code (.cpp or .mm) on this framework, Chris Moore (on the comments) advises to use the 「-lstdc++」 option. In this case could be a good idea to use 「-ObjC」 too, to avoid conflicts in old compilers.
  • Wrapper Extension: framework. Here we change the Bundle to a Framework. To Xcode, frameworks is just a folder with the extension .framework, which has inside one or more compiled binary sources, resources and some folders, a folder, usually called Headers, contains all the public headers.
  • Generate Debug Symbols: NO (this is a very important setting, otherwise the framework will not work on other computers/profiles).
  • Precompile Prefix Header: NO.
  • Prefix Header: 「」. (Leave it blank).

IMPORTANT: Since the Xcode 4.x the architectures armv6 has no longer support. So, to create a real Universal Framework we must make a small 「hack」:iphone

  1. After change the settings above close the Xcode, find the .xcodeproj (the project file) in Finder and then 「Show Package Contents」.
  2. Open the file 「project.pbxproj」 into a text editor.
  3. Delete all the lines with VALID_ARCHS = 「$(ARCHS_STANDARD_32_BIT)」.

 

3 接下來在build phase 中添加各類資源,包括 Copy Bundle Resources,Compile Sources。要注意的是添加 Copy Headers,是區分開放和私有的。ide

  • Public: Headers that other developers must know in order to work with your framework. In the final framework product, these headers will be visible even to Xcode.
  • Private: Headers that is not necessary to other developers, but is good for consult or for reference. These headers will not be visible to Xcode, but will be in the framework folder.
  • Project: Headers that the other developers nor Xcode have access. In reality these headers will not be placed in the final product, this is just to instruct the compiler to create your custom framework. 

Tip: To add many files at once, click on the 「+」 button and write the files’ extension on the search field. For example 「.m」, 「.c」, 「.cpp」, 「.h」, etc. This can save a lot of time. (搜索後綴,能夠節省很多時間。。)ui

4 接下來創建target,用來生成通用的framework。this

在aggregate的build phases中添加run script:idea

# Sets the target folders and the final framework product.
FMK_NAME=FI
FMK_VERSION=A
 
# Install dir will be the final output to the framework.
# The following line create it in the root folder of the current project.
INSTALL_DIR=${SRCROOT}/Products/${FMK_NAME}.framework
 
# Working dir will be deleted after the framework creation.
WRK_DIR=build
DEVICE_DIR=${WRK_DIR}/Release-iphoneos/${FMK_NAME}.framework
SIMULATOR_DIR=${WRK_DIR}/Release-iphonesimulator/${FMK_NAME}.framework
 
# Building both architectures.
xcodebuild -configuration "Release" -target "${FMK_NAME}" -sdk iphoneos
xcodebuild -configuration "Release" -target "${FMK_NAME}" -sdk iphonesimulator
 
# Cleaning the oldest.
if [ -d "${INSTALL_DIR}" ]
then
rm -rf "${INSTALL_DIR}"
fi
 
# Creates and renews the final product folder.
mkdir -p "${INSTALL_DIR}"
mkdir -p "${INSTALL_DIR}/Versions"
mkdir -p "${INSTALL_DIR}/Versions/${FMK_VERSION}"
mkdir -p "${INSTALL_DIR}/Versions/${FMK_VERSION}/Resources"
mkdir -p "${INSTALL_DIR}/Versions/${FMK_VERSION}/Headers"
 
# Creates the internal links.
# It MUST uses relative path, otherwise will not work when the folder is copied/moved.
ln -s "${FMK_VERSION}" "${INSTALL_DIR}/Versions/Current"
ln -s "Versions/Current/Headers" "${INSTALL_DIR}/Headers"
ln -s "Versions/Current/Resources" "${INSTALL_DIR}/Resources"
ln -s "Versions/Current/${FMK_NAME}" "${INSTALL_DIR}/${FMK_NAME}"
 
# Copies the headers and resources files to the final product folder.
cp -R "${DEVICE_DIR}/Headers/" "${INSTALL_DIR}/Versions/${FMK_VERSION}/Headers/"
cp -R "${DEVICE_DIR}/" "${INSTALL_DIR}/Versions/${FMK_VERSION}/Resources/"
 
# Removes the binary and header from the resources folder.
rm -r "${INSTALL_DIR}/Versions/${FMK_VERSION}/Resources/Headers" "${INSTALL_DIR}/Versions/${FMK_VERSION}/Resources/${FMK_NAME}"
 
# Uses the Lipo Tool to merge both binary files (i386 + armv6/armv7) into one Universal final product.
lipo -create "${DEVICE_DIR}/${FMK_NAME}" "${SIMULATOR_DIR}/${FMK_NAME}" -output "${INSTALL_DIR}/Versions/${FMK_VERSION}/${FMK_NAME}"
 
rm -r "${WRK_DIR}"

最後,build這個aggregate的target就能夠了!!!spa

#################################

遇到的問題:

新建的bundle找不到Apple LLVM compiler的選項了,簡直就是我累個擦啊!最後在原文的評論中發現了:先編譯一遍,那些選項就回出現了。。。設置好了,從新build就能夠了。。

相關文章
相關標籤/搜索