一般我們在xcode里面配置包含工程目錄下頭文件的時候腹缩,都要關(guān)聯(lián)著相對路徑和絕對路徑,如果只是自己用這個項目卵凑,用絕對路徑的問題不大庆聘,但是如果協(xié)作開發(fā),這時候絕對路徑的缺點立馬出現(xiàn)勺卢。
所以在修改User Header Search Paths這個選項的時候使用
"$(SRCROOT)/當(dāng)前工程名字/需要包含頭文件所在文件夾"
將上面的雙引號里面的字符串拷貝之后伙判,你會發(fā)現(xiàn)這個“$(SRCROOT)”,會自動變成當(dāng)前工程所以的目錄黑忱。
一宴抚、xcode中的環(huán)境變量
$(BUILT_PRODUCTS_DIR)
build成功后的,最終產(chǎn)品路徑--可以在Build Settings參數(shù)的Per-configuration Build Products Path項里設(shè)置
$(TARGET_NAME)
目標(biāo)工程名稱
$(SRCROOT)
工程根目錄(一般為.xcodeproj所在目錄)
$(PROJECT_DIR)
.xcodeproj所在目錄
$(CURRENT_PROJECT_VERSION)
當(dāng)前工程版本號
其他:
$() xcode數(shù)據(jù)目錄該工程根目錄
$(SYMROOT) = $()/Build/Products
$(BUILD_DIR) = $()/Build/Products
$(BUILD_ROOT) = $()/Build/Products
這三個變量中的$()不會隨著Build Settings參數(shù)的設(shè)置而改變
相反甫煞,以下可以通過設(shè)置而改變
$(CONFIGURATION_BUILD_DIR) = $()/Build/Products/Debug-iphonesimulator
$(BUILT_PRODUCTS_DIR) = $()/Build/Products/Debug-iphonesimulator
$(CONFIGURATION_TEMP_DIR) = $()/Build/Intermediates/UtilLib.build/Debug-iphonesimulator
$(TARGET_BUILD_DIR) = $()/Build/Products/Debug-iphonesimulator
$(SDK_NAME) = iphonesimulator5.0
$(PLATFORM_NAME) = iphonesimulator
$(CONFIGURATION) = Debug
$(TARGET_NAME) = UtilLib
$(EXECUTABLE_NAME) = libUtilLib.a 可執(zhí)行文件名
${IPHONEOS_DEPLOYMENT_TARGET} 5.0
$(ACTION) = build
$(CURRENTCONFIG_SIMULATOR_DIR) 當(dāng)前模擬器路徑
$(CURRENTCONFIG_DEVICE_DIR) 當(dāng)前設(shè)備路徑
$(BUILD_DIR)/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME =
$()/Build/Products/Debug-iphonesimulator
$(PROJECT_TEMP_DIR)/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME) = $()/Build/Intermediates/UtilLib.build/Debug-iphonesimulator
自定義變量
${CONFIGURATION}-iphoneos 表示:Debug-iphoneos
${CONFIGURATION}-iphonesimulator 表示:Debug-iphonesimulator
$(CURRENTCONFIG_DEVICE_DIR) = ${SYMROOT}/${CONFIGURATION}-iphoneos
$(CURRENTCONFIG_SIMULATOR_DIR) = ${SYMROOT}/${CONFIGURATION}-iphonesimulator
自定義一個設(shè)備無關(guān)的路徑(用來存放各種架構(gòu)arm6/arm7/i386輸出的產(chǎn)品)
$(CREATING_UNIVERSAL_DIR) = ${SYMROOT}/${CONFIGURATION}-universal
自定義變量代表的值
$(CURRENTCONFIG_DEVICE_DIR) = $()/Build/Products/Debug-iphoneos
$(CURRENTCONFIG_SIMULATOR_DIR) = $()/Build/Products/Debug-iphonesimulator
$(CREATING_UNIVERSAL_DIR) = $()/Build/Products/Debug-universal
iphoneos5.0下的編譯腳本:
xcodebuild -project "UtilLib.xcodeproj" -configuration "Debug" -target "UtilLib" -sdk "iphoneos5.0" -arch "armv6 armv7" build RUN_CLANG_STATIC_ANALYZER=NO $(BUILD_DIR)="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}"
iphonesimulator5.0下的編譯腳本:
xcodebuild -project "UtilLib.xcodeproj" -configuration "Debug" -target "UtilLib" -sdk "iphonesimulator5.0" -arch "i386" build RUN_CLANG_STATIC_ANALYZER=NO $(BUILD_DIR)="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}"
加上下面一句表示輸出到文件:
"${BUILD_ROOT}.build_output"
lipo腳本工具:合并iPhone模擬器和真機的靜態(tài)類庫菇曲,生成通用庫
lipo -create -output "${CREATING_UNIVERSAL_DIR}/${EXECUTABLE_NAME}" "${CURRENTCONFIG_DEVICE_DIR}/${EXECUTABLE_NAME}" "${CURRENTCONFIG_SIMULATOR_DIR}/${EXECUTABLE_NAME}"
意思是:把"${CURRENTCONFIG_DEVICE_DIR}目錄下的.a文件,和${CURRENTCONFIG_SIMULATOR_DIR}目錄下的.a文件合并抚吠,
在${CREATING_UNIVERSAL_DIR}目錄下常潮,生成兩個設(shè)備都通用的靜態(tài)庫,
例如:lipo -create -output xy.a x.a y.a
二、xcode中build Settings常見參數(shù)解析
1.Installation Directory:安裝路徑
靜態(tài)庫編譯時楷力,在Build Settings中Installation Directory設(shè)置“$(BUILT_PRODUCTS_DIR)”
Skip Install設(shè)為YES
Installation Directory默認(rèn)為/usr/local/lib
因為Build Location默認(rèn)時喊式,.a文件會放在很長(比如:/Users/xxx/Library/Developer/Xcode/DerivedData/xxxProgram
dalrvzehhtesxdfqhxixzafvddwe/Build/Products/Debug-iPhoneos)的路徑下,或是我們target指定的路徑
Skip Install如果是NO,可能會被安裝到默認(rèn)路徑/usr/local/lib
2.Public Headers Folder Path:對外公開頭文件路徑
設(shè)為“include”(具體的頭文件路徑為:$(BUILT_PRODUCTS_DIR)/include/xx.h)
在最終文件.a同級目錄下生成一個include目錄
默認(rèn):/usr/local/include
Public Headers Folder Path這個路徑就是使用這lib的某工程需要依賴的外部頭文件.導(dǎo)入這路徑后萧朝,#include/import "xx.h"才能看到
3.User Header Search Paths:依賴的外部頭文件搜索路徑
設(shè)置為“$(BUILT_PRODUCTS_DIR)/include”
和2中路徑對應(yīng)
4.Per-configuration Build Products Path:最終文件路徑
比如設(shè)為“../app”岔留,就會在工程文件.xcodeproj上一層目錄下的app目錄里,創(chuàng)建最終文件
默認(rèn)為$(BUILD_DIR)/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)
等于$(BUILT_PRODUCTS_DIR)
5.Per-configuration Intermediate Build Files Path:臨時中間文件路徑
默認(rèn)為:$(PROJECT_TEMP_DIR)/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)
6.Code Signing Identity:真機調(diào)試的證書選擇
選一個和Bundle identifier相對應(yīng)的證書
Library Search Paths:庫搜索路徑
Architectures:架構(gòu)检柬,設(shè)為 armv6 或 armv7
Valid Architectures:應(yīng)用框架献联,可以設(shè)為 armv6、 armv7 或i386
Product Name:工程文件名,默認(rèn)為$(TARGET_NAME)
Info.plist File:info文件路徑
Build Variants:默認(rèn)為normal
Other Linker Flags:其他鏈接標(biāo)簽
設(shè)為“-ObjC”
當(dāng)導(dǎo)入的靜態(tài)庫使用了類別里逆,需要設(shè)為-ObjC
iOS Deployment Target:ios部署對象
比如可以選擇設(shè)為进胯,ios3到ios5的一種版本
Prefix Header:預(yù)編頭文件(比如:UtilLib/UtilLib-Prefix.pch)
Precompile Prefix Header:設(shè)為“Yes”,表示允許加入預(yù)編譯頭
三运悲、workspace(工作區(qū))
作用:管理多個工程(project)龄减,多工程聯(lián)編
四、workspace多工程聯(lián)編設(shè)置
一班眯、
1.新建一個靜態(tài)庫工程希停,比如UtilLib,并生成UtilLib.h和UtilLib.m文件
2.選中需要公開的頭文件署隘,
把右側(cè)欄的Target Membership中設(shè)置為public
或則宠能,選中工程目錄target的Build Phases標(biāo)簽的copy headers項,在public中添加要公開的頭文件
3.Architectures設(shè)為:armv6 armv7
4.Valid Architectures設(shè)為:armv6 armv7 i386
5.Build Products Path設(shè)為:$(SRCROOT)/../build
6.Per-configuration Build Products Path設(shè)為:
$(SRCROOT)/../build/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)
7.Per-configuration Intermediate Build Files Path設(shè)為:
$(SRCROOT)/../build/$(TARGET_NAME).build/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)
8.設(shè)置安裝路徑:Installation Directory項
9.設(shè)置對外公開的頭文件路徑:Public Headers Folder Path項
10.為靜態(tài)庫添加依賴的shell腳本
選中工程目錄target的Build Phases標(biāo)簽磁餐,點擊由下角的Add Build Phase按鈕
在彈出的菜單里選擇Add run script項违崇,然后頁面中會多出一個Run Script項
在黑框里填寫"$SRCROOT/mergeArmSymbols.sh"
建立對此腳本的依賴(編譯靜態(tài)庫的后會運行此腳本)
如果編譯時設(shè)備選的是iphone simulator:
則此腳本會在對應(yīng)iphone device的產(chǎn)品目錄Debug-iphoneos中,生成對device有用的.a靜態(tài)庫诊霹,
相反羞延,如果設(shè)備選的是iphone device:
則此腳本會在對應(yīng)iphone simulator的產(chǎn)品目錄Debug-iphoneos中,生成對simulator有用的.a靜態(tài)庫
最后脾还,此腳本調(diào)用lipo工具伴箩,把本工程生成靜態(tài)庫與此腳本生成的靜態(tài)庫合并,生成simulator和device都通用的.a文件
11.具體bash shell腳本如下:
[plain] view plaincopy
mergeArmSymbols.sh
Version 2.0 (updated for Xcode 4, with some fixes)
Author: Adam Martin - http://twitter.com/redglassesapps
Based on: original script from Eonil (main changes: Eonil's script WILL NOT WORK in Xcode GUI - it WILL CRASH YOUR COMPUTER)
More info: see this Stack Overflow question: http://stackoverflow.com/questions/3520977/build-fat-static-library-device-simulator-using-xcode-and-sdk-4
#################[ Tests: helps workaround any future bugs in Xcode ]########
DEBUG_THIS_SCRIPT="true"
if [ $DEBUG_THIS_SCRIPT = "true" ]
then
echo "########### TESTS #############"
echo "Use the following variables when debugging this script; note that they may change on recursions"
echo "BUILD_DIR = $BUILD_DIR"
echo "BUILD_ROOT = $BUILD_ROOT"
echo "CONFIGURATION_BUILD_DIR = $CONFIGURATION_BUILD_DIR"
echo "BUILT_PRODUCTS_DIR = $BUILT_PRODUCTS_DIR"
echo "CONFIGURATION_TEMP_DIR = $CONFIGURATION_TEMP_DIR"
echo "TARGET_BUILD_DIR = $TARGET_BUILD_DIR"
echo "SDK_NAME = $SDK_NAME"
echo "PLATFORM_NAME = $PLATFORM_NAME"
echo "CONFIGURATION = $CONFIGURATION"
echo "TARGET_NAME = $TARGET_NAME"
echo "ARCH_TO_BUILD = $ARCH_TO_BUILD"
echo "ARCH_TO_BUILD = $ARCH_TO_BUILD"
echo "ACTION = $ACTION"
echo "SYMROOT = $SYMROOT"
echo "EXECUTABLE_NAME = $EXECUTABLE_NAME"
echo "CURRENTCONFIG_SIMULATOR_DIR = $CURRENTCONFIG_SIMULATOR_DIR"
echo "CURRENTCONFIG_DEVICE_DIR = $CURRENTCONFIG_DEVICE_DIR"
echo "#############Other###########"
echo "BUILD_DIR/CONFIGURATION/EFFECTIVE_PLATFORM_NAME = $BUILD_DIR/$CONFIGURATION$EFFECTIVE_PLATFORM_NAME"
echo "PROJECT_TEMP_DIR/CONFIGURATION/EFFECTIVE_PLATFORM_NAME = $PROJECT_TEMP_DIR/$CONFIGURATION$EFFECTIVE_PLATFORM_NAME"
fi
#####################[ part 1 ]##################
First, work out the BASESDK version number
(incidental: searching for substrings in sh is a nightmare! Sob)
SDK_VERSION=$(echo ${SDK_NAME} | grep -o '.{3}$')
Next, work out if we're in SIM or DEVICE
if [ ${PLATFORM_NAME} = "iphonesimulator" ]
then
OTHER_SDK_TO_BUILD=iphoneos${SDK_VERSION}
ARCH_TO_BUILD="armv6 armv7"
else
OTHER_SDK_TO_BUILD=iphonesimulator${SDK_VERSION}
ARCH_TO_BUILD="i386"
fi
echo "XCode has selected SDK: ${PLATFORM_NAME} with version: ${SDK_VERSION} (although back-targetting: ${IPHONEOS_DEPLOYMENT_TARGET})"
echo "...therefore, OTHER_SDK_TO_BUILD = ${OTHER_SDK_TO_BUILD}"
#####################[ end of part 1 ]##################
#####################[ part 2 ]##################
IF this is the original invocation, invoke whatever other builds are required
Xcode is already building ONE target... build ONLY the missing platforms/configurations.
if [ "true" == ${ALREADYINVOKED:-false} ]
then
echo "RECURSION: Not the root invocation, don't recurse"
else
Prevent recursion
export ALREADYINVOKED="true"
echo "RECURSION: I am the root... recursing all missing build targets..."
echo "RECURSION: ...about to invoke: xcodebuild -configuration "${CONFIGURATION}" -target "${TARGET_NAME}" -sdk "${OTHER_SDK_TO_BUILD}" -arch "${ARCH_TO_BUILD}" ${ACTION} RUN_CLANG_STATIC_ANALYZER=NO"
xcodebuild -project "${TARGET_NAME}.xcodeproj" -configuration "${CONFIGURATION}" -target "${TARGET_NAME}" -sdk "${OTHER_SDK_TO_BUILD}" -arch "${ARCH_TO_BUILD}" ${ACTION} RUN_CLANG_STATIC_ANALYZER=NO BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}" > "${BUILD_ROOT}.build_output"
ACTION="build"
Merge all platform binaries as a fat binary for each configurations.
Calculate where the (multiple) built files are coming from:
CURRENTCONFIG_DEVICE_DIR=${SRCROOT}/../build/${CONFIGURATION}-iphoneos
CURRENTCONFIG_SIMULATOR_DIR=${SRCROOT}/../build/${CONFIGURATION}-iphonesimulator
echo "Taking device build from: ${CURRENTCONFIG_DEVICE_DIR}"
echo "Taking simulator build from: ${CURRENTCONFIG_SIMULATOR_DIR}"
CREATING_UNIVERSAL_DIR=${SRCROOT}/../build/${CONFIGURATION}-universal
echo "...outputing a universal arm6/arm7/i386 build to: ${CREATING_UNIVERSAL_DIR}"
... remove the products of previous runs of this script
NB: this directory is only created by this script - it should be safe to delete
rm -rf "${CREATING_UNIVERSAL_DIR}"
mkdir "${CREATING_UNIVERSAL_DIR}"
echo "lipo: for current configuration (${CONFIGURATION}) creating output file: ${CREATING_UNIVERSAL_DIR}/${EXECUTABLE_NAME}"
lipo -create -output "${CREATING_UNIVERSAL_DIR}/${EXECUTABLE_NAME}" "${CURRENTCONFIG_DEVICE_DIR}/${EXECUTABLE_NAME}" "${CURRENTCONFIG_SIMULATOR_DIR}/${EXECUTABLE_NAME}"
#######custom########
copy universal lib to ../libs
libsDir=../libs
includeDir=../include
rm -rf "${libsDir}"
mkdir -p "${libsDir}"
echo "cp -R ${CREATING_UNIVERSAL_DIR}/${EXECUTABLE_NAME} ${libsDir}"
cp -R "${CREATING_UNIVERSAL_DIR}/${EXECUTABLE_NAME}" "${libsDir}"
echo "cp -R ${CURRENTCONFIG_DEVICE_DIR}/include ${includeDir}"
cp -R "${CURRENTCONFIG_DEVICE_DIR}/include" "${includeDir}"
fi
下載右邊的圖片鄙漏,然后把后綴改為.sh(其實就是上面的腳本嗤谚,因為博客園只能上傳圖片)
靜態(tài)庫編譯后的目錄結(jié)構(gòu)如下:
二、
1.新建主工程怔蚌,比如Nuno巩步,添加對靜態(tài)庫的依賴
點擊工程,在Build Phases標(biāo)簽的Link Binary With Libraries項中點擊加號添加UtilLib.a庫
選中上面的紅色項桦踊,在右邊欄的Location選Relative to Project椅野,把值設(shè)為../libs/libUtilLib.a
2.設(shè)置主工程依賴的外部頭文件路徑:User Header Search Paths項
$(SRCROOT)/../include
3.設(shè)置Header Search Paths為:$(SRCROOT)/../include
4.設(shè)置Library Search Paths為:$(SRCROOT)/../libs
編譯運行即可實現(xiàn)聯(lián)編
(備注:選擇模擬器iphone 5.0 simulator,編譯靜態(tài)庫的時籍胯,最終文件會在Debug-iphonesimulator鳄橘,就算成功.a文件還是紅色,
這是可能是xcode的bug芒炼,不會自動切換路徑
因為$(BUILT_PRODUCTS_DIR)所指的位置,是build/Debug-iphonesos,不是包含最終.a文件的Debug-iphonesimulator术徊;
選擇ios Device本刽,編譯成的最終文件才在build/Debug-iphonesos下,.a文件變成非紅色
所有得用mergeArmSymbols.sh腳本來解決)