xcode 路徑

$(SRCROOT)宏和$(PROJECT_DIR)宏html

XCode環境變量及路徑設置

分類: Objective-C2013-03-11 12:30 41336人閱讀 評論(1) 收藏 舉報 通常咱們在xcode裏面配置包含工程目錄下頭文件的時候,都要關聯着相對路徑和絕對路徑,若是隻是本身用這個項目,用絕對路徑的問題不大,可是若是你把 工程發給別人,別人就要在改這個絕對路徑,這時候絕對路徑的缺點立馬出現。

因此在修改User Header Search Paths這個選項的時候使用
"$(SRCROOT)/當前工程名字/須要包含頭文件所在文件夾"
將上面的雙引號裏面的字符串拷貝以後,你會發現這個「$(SRCROOT)」,會自動變成當前工程因此的目錄。java

這樣就能夠了,發給別人,別人也不用在去修改路徑了。ios

 

 

 

 

 

xcode4的環境變量,Build Settings參數,workspace及聯編設置

1、xcode4中的環境變量shell

?
1
$(BUILT_PRODUCTS_DIR)

build成功後的,最終產品路徑--能夠在Build Settings參數的Per-configuration Build Products Path項裏設置xcode

?
1
$(TARGET_NAME)

目標工程名稱bash

?
1
$(SRCROOT)

工程文件(好比Nuno.xcodeproj)的路徑架構

?
1
$(CURRENT_PROJECT_VERSION)

當前工程版本號app

 

其餘:框架

當編譯靜態庫,設備選模擬器(iPhone 5.0 Simulator),未設置任何Build Settings參數時,默認的基礎路徑:iphone

/Users/xxx/Library/Developer/Xcode/DerivedData/xxxWorkspace-caepeadwrerdcrftijaolkkagbjf

下面用$()代替上面一長串東東

$(SYMROOT) = $()/Build/Products

$(BUILD_DIR) = $()/Build/Products

$(BUILD_ROOT) = $()/Build/Products

這三個變量中的$()不會隨着Build Settings參數的設置而改變

相反,如下能夠經過設置而改變

$(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 可執行文件名

${IPHONEOS_DEPLOYMENT_TARGET} 5.0

$(ACTION) = build

$(CURRENTCONFIG_SIMULATOR_DIR) 當前模擬器路徑

$(CURRENTCONFIG_DEVICE_DIR) 當前設備路徑

 

$(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

自定義一個設備無關的路徑(用來存放各類架構arm6/arm7/i386輸出的產品)

$(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模擬器和真機的靜態類庫,生成通用庫

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}目錄下,生成兩個設備都通用的靜態庫,

例如:lipo -create -output xy.a x.a y.a

 

2、xcode4中build Settings常見參數解析

1.Installation Directory:安裝路徑

靜態庫編譯時,在Build Settings中Installation Directory設置「$(BUILT_PRODUCTS_DIR)」

Skip Install設爲YES

Installation Directory默認爲/usr/local/lib

由於Build Location默認時,.a文件會放在很長(好比:/Users/xxx/Library/Developer/Xcode/DerivedData/xxxProgram

dalrvzehhtesxdfqhxixzafvddwe/Build/Products/Debug-iPhoneos)的路徑下,或是咱們target指定的路徑

Skip Install若是是NO,可能會被安裝到默認路徑/usr/local/lib

2.Public Headers Folder Path:對外公開頭文件路徑

設爲「include」(具體的頭文件路徑爲:$(BUILT_PRODUCTS_DIR)/include/xx.h)

在最終文件.a同級目錄下生成一個include目錄

默認:/usr/local/include

Public Headers Folder Path這個路徑就是使用這lib的某工程須要依賴的外部頭文件.導入這路徑後,#include/import "xx.h"才能看到

3.User Header Search Paths:依賴的外部頭文件搜索路徑

設置爲「$(BUILT_PRODUCTS_DIR)/include」

和2中路徑對應

4.Per-configuration Build Products Path:最終文件路徑

好比設爲「../app」,就會在工程文件.xcodeproj上一層目錄下的app目錄裏,建立最終文件

默認爲$(BUILD_DIR)/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)

等於$(BUILT_PRODUCTS_DIR)

5.Per-configuration Intermediate Build Files Path:臨時中間文件路徑

默認爲:$(PROJECT_TEMP_DIR)/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)

6.Code Signing Identity:真機調試的證書選擇

選一個和Bundle identifier相對應的證書

Library Search Paths:庫搜索路徑

Architectures:架構,設爲 armv6 或 armv7

Valid Architectures:應用框架,能夠設爲 armv六、 armv7 或i386

Product Name:工程文件名,默認爲$(TARGET_NAME)

Info.plist File:info文件路徑

Build Variants:默認爲normal

Other Linker Flags:其餘連接標籤

設爲「-ObjC」

當導入的靜態庫使用了類別,須要設爲-ObjC

iOS Deployment Target:ios部署對象

好比能夠選擇設爲,ios3到ios5的一種版本

Prefix Header:預編頭文件(好比:UtilLib/UtilLib-Prefix.pch)

Precompile Prefix Header:設爲「Yes」,表示容許加入預編譯頭

 

3、workspace(工做區)

做用:管理多個工程(project),多工程聯編

 

4、workspace多工程聯編設置

1、

1.新建一個靜態庫工程,好比UtilLib,並生成UtilLib.h和UtilLib.m文件

2.選中須要公開的頭文件,

\

把右側欄的Target Membership中設置爲public

\

或則,選中工程目錄target的Build Phases標籤的copy headers項,在public中添加要公開的頭文件

\

3.Architectures設爲:armv6 armv7

4.Valid Architectures設爲:armv6 armv7 i386

5.Build Products Path設爲:$(SRCROOT)/../build

6.Per-configuration Build Products Path設爲:

$(SRCROOT)/../build/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)

7.Per-configuration Intermediate Build Files Path設爲:

$(SRCROOT)/../build/$(TARGET_NAME).build/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)

8.設置安裝路徑:Installation Directory項

\

9.設置對外公開的頭文件路徑:Public Headers Folder Path項

\

 

10.爲靜態庫添加依賴的shell腳本

選中工程目錄target的Build Phases標籤,點擊由下角的Add Build Phase按鈕

\

在彈出的菜單裏選擇Add run script項,而後頁面中會多出一個Run Script項

\

在黑框裏填寫"$SRCROOT/mergeArmSymbols.sh"

\

創建對此腳本的依賴(編譯靜態庫的後會運行此腳本)

若是編譯時設備選的是iphone simulator:

則此腳本會在對應iphone device的產品目錄Debug-iphoneos中,生成對device有用的.a靜態庫,

相反,若是設備選的是iphone device:

則此腳本會在對應iphone simulator的產品目錄Debug-iphoneos中,生成對simulator有用的.a靜態庫

最後,此腳本調用lipo工具,把本工程生成靜態庫與此腳本生成的靜態庫合併,生成simulator和device都通用的.a文件

 

11.具體bash shell腳本以下:

 

[plain] view plaincopy

    1. mergeArmSymbols.sh
    2. # Version 2.0 (updated for Xcode 4, with some fixes)
    3.  
    4. # Author: Adam Martin - http://twitter.com/redglassesapps
    5. # Based on: original script from Eonil (main changes: Eonil's script WILL NOT WORK in Xcode GUI - it WILL CRASH YOUR COMPUTER)
    6. #
    7. # More info: see this Stack Overflow question: http://stackoverflow.com/questions/3520977/build-fat-static-library-device-simulator-using-xcode-and-sdk-4
    8.  
    9. #################[ Tests: helps workaround any future bugs in Xcode ]########
    10. #
    11. DEBUG_THIS_SCRIPT="true"
    12.  
    13. if [ $DEBUG_THIS_SCRIPT = "true" ]
    14. then
    15. echo "########### TESTS #############"
    16. echo "Use the following variables when debugging this script; note that they may change on recursions"
    17. echo "BUILD_DIR = $BUILD_DIR"
    18. echo "BUILD_ROOT = $BUILD_ROOT"
    19. echo "CONFIGURATION_BUILD_DIR = $CONFIGURATION_BUILD_DIR"
    20. echo "BUILT_PRODUCTS_DIR = $BUILT_PRODUCTS_DIR"
    21. echo "CONFIGURATION_TEMP_DIR = $CONFIGURATION_TEMP_DIR"
    22. echo "TARGET_BUILD_DIR = $TARGET_BUILD_DIR"
    23. echo "SDK_NAME = $SDK_NAME"
    24. echo "PLATFORM_NAME = $PLATFORM_NAME"
    25. echo "CONFIGURATION = $CONFIGURATION"
    26. echo "TARGET_NAME = $TARGET_NAME"
    27. echo "ARCH_TO_BUILD = $ARCH_TO_BUILD"
    28. echo "ARCH_TO_BUILD = $ARCH_TO_BUILD"
    29. echo "ACTION = $ACTION"
    30. echo "SYMROOT = $SYMROOT"
    31. echo "EXECUTABLE_NAME = $EXECUTABLE_NAME"
    32. echo "CURRENTCONFIG_SIMULATOR_DIR = $CURRENTCONFIG_SIMULATOR_DIR"
    33. echo "CURRENTCONFIG_DEVICE_DIR = $CURRENTCONFIG_DEVICE_DIR"
    34.  
    35. echo "#############Other###########"
    36. echo "BUILD_DIR/CONFIGURATION/EFFECTIVE_PLATFORM_NAME = $BUILD_DIR/$CONFIGURATION$EFFECTIVE_PLATFORM_NAME"
    37.  
    38. echo "PROJECT_TEMP_DIR/CONFIGURATION/EFFECTIVE_PLATFORM_NAME = $PROJECT_TEMP_DIR/$CONFIGURATION$EFFECTIVE_PLATFORM_NAME"
    39.  
    40. fi
    41.  
    42. #####################[ part 1 ]##################
    43. # First, work out the BASESDK version number
    44. # (incidental: searching for substrings in sh is a nightmare! Sob)
    45.  
    46. SDK_VERSION=$(echo ${SDK_NAME} | grep -o '.\{3\}$')
    47.  
    48. # Next, work out if we're in SIM or DEVICE
    49.  
    50. if [ ${PLATFORM_NAME} = "iphonesimulator" ]
    51. then
    52. OTHER_SDK_TO_BUILD=iphoneos${SDK_VERSION}
    53. ARCH_TO_BUILD="armv6 armv7"
    54. else
    55. OTHER_SDK_TO_BUILD=iphonesimulator${SDK_VERSION}
    56. ARCH_TO_BUILD="i386"
    57. fi
    58.  
    59. echo "XCode has selected SDK: ${PLATFORM_NAME} with version: ${SDK_VERSION} (although back-targetting: ${IPHONEOS_DEPLOYMENT_TARGET})"
    60. echo "...therefore, OTHER_SDK_TO_BUILD = ${OTHER_SDK_TO_BUILD}"
    61. #
    62. #####################[ end of part 1 ]##################
    63.  
    64. #####################[ part 2 ]##################
    65. #
    66. # IF this is the original invocation, invoke whatever other builds are required
    67. #
    68. # Xcode is already building ONE target... build ONLY the missing platforms/configurations.
    69.  
    70. if [ "true" == ${ALREADYINVOKED:-false} ]
    71. then
    72. echo "RECURSION: Not the root invocation, don't recurse"
    73. else
    74. # Prevent recursion
    75. export ALREADYINVOKED="true"
    76.  
    77. echo "RECURSION: I am the root... recursing all missing build targets..."
    78. 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"
    79. 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"
    80. ACTION="build"
    81.  
    82. # Merge all platform binaries as a fat binary for each configurations.
    83.  
    84. # Calculate where the (multiple) built files are coming from:
    85. CURRENTCONFIG_DEVICE_DIR=${SRCROOT}/../build/${CONFIGURATION}-iphoneos
    86. CURRENTCONFIG_SIMULATOR_DIR=${SRCROOT}/../build/${CONFIGURATION}-iphonesimulator
    87.  
    88. echo "Taking device build from: ${CURRENTCONFIG_DEVICE_DIR}"
    89. echo "Taking simulator build from: ${CURRENTCONFIG_SIMULATOR_DIR}"
    90.  
    91. CREATING_UNIVERSAL_DIR=${SRCROOT}/../build/${CONFIGURATION}-universal
    92. echo "...outputing a universal arm6/arm7/i386 build to: ${CREATING_UNIVERSAL_DIR}"
    93.  
    94. # ... remove the products of previous runs of this script
    95. # NB: this directory is only created by this script - it should be safe to delete
    96.  
    97. rm -rf "${CREATING_UNIVERSAL_DIR}"
    98. mkdir "${CREATING_UNIVERSAL_DIR}"
    99.  
    100. #
    101. echo "lipo: for current configuration (${CONFIGURATION}) creating output file: ${CREATING_UNIVERSAL_DIR}/${EXECUTABLE_NAME}"
    102. lipo -create -output "${CREATING_UNIVERSAL_DIR}/${EXECUTABLE_NAME}" "${CURRENTCONFIG_DEVICE_DIR}/${EXECUTABLE_NAME}" "${CURRENTCONFIG_SIMULATOR_DIR}/${EXECUTABLE_NAME}"
    103.  
    104. #######custom########
    105. #copy universal lib to ../libs
    106. libsDir=../libs
    107. includeDir=../include
    108.  
    109. rm -rf "${libsDir}"
    110. mkdir -p "${libsDir}"
    111.  
    112. echo "cp -R ${CREATING_UNIVERSAL_DIR}/${EXECUTABLE_NAME} ${libsDir}"
    113.  
    114. cp -R "${CREATING_UNIVERSAL_DIR}/${EXECUTABLE_NAME}" "${libsDir}"
    115.  
    116. echo "cp -R ${CURRENTCONFIG_DEVICE_DIR}/include ${includeDir}"
    117.  
    118. cp -R "${CURRENTCONFIG_DEVICE_DIR}/include" "${includeDir}"
    119.  
    120. fi

       

      下載右邊的圖片加載中...,而後把後綴改成.sh(其實就是上面的腳本,由於博客園只能上傳圖片)

      靜態庫編譯後的目錄結構以下:

      \

      \

       

      2、

      1.新建主工程,好比Nuno,添加對靜態庫的依賴

      點擊工程,在Build Phases標籤的Link Binary With Libraries項中點擊加號添加UtilLib.a庫

      \

      \

      \

      選中上面的紅色項,在右邊欄的Location選Relative to Project,把值設爲../libs/libUtilLib.a

      2.設置主工程依賴的外部頭文件路徑:User Header Search Paths項

      $(SRCROOT)/../include

      3.設置Header Search Paths爲:$(SRCROOT)/../include

      4.設置Library Search Paths爲:$(SRCROOT)/../libs

      編譯運行便可實現聯編

       

      (備註:選擇模擬器iphone 5.0 simulator,編譯靜態庫的時,最終文件會在Debug-iphonesimulator,就算成功.a文件仍是紅色,

      這是多是xcode的bug,不會自動切換路徑

      由於$(BUILT_PRODUCTS_DIR)所指的位置,是build/Debug-iphonesos,不是包含最終.a文件的Debug-iphonesimulator;

      選擇ios Device,編譯成的最終文件纔在build/Debug-iphonesos下,.a文件變成非紅色

      全部得用mergeArmSymbols.sh腳原本解決)

相關文章
相關標籤/搜索