天天看點

macOS常用二進制工具xcodebuildxcrunlibtoolarliponmotoolobjdumpintstall_name_tool

macOS平台下Framework開發過程中經常需要用到各種工具,下面做個總結以防遺忘。每個工具指令隻舉幾個例子,詳細使用幫助可以通過man查詢。

xcodebuild

build Xcode projects and workspaces

這個工具可以用來編譯xcode工程:

xcodebuild -project MyFramework.xcodeproj -scheme MyFramework -configuration Release CONFIGURATION_BUILD_DIR='~/Desktop/build/arm'
           

xcrun

Run or locate development tools and properties.

通常用xcrun來生成ipa包:

xcrun -sdk iphoneos -v PackageApplication ./build/Release-iphoneos/Toyun.app -o ~/Desktop/Toyun.ipa
           

libtool

create libraries

建立庫。mac平台下的Framework依賴另外一個Framework,編譯後預設是不會合并成一個的,如果想要給出一個Framework可以使用這個工具。

合并兩個靜态庫:

libtool -static -o c a.framework/a b.framework/b

           

ar

create and maintain library archives

這個指令用于建立和管理歸檔(archive)檔案。主要應用是解決第三方庫沖突,例如ffmpeg沖突就可以用ar分離出沖突檔案,并打包。

将.o從.a靜态庫中删除:

ar -d lib.a conflict.o
           

将.a檔案解壓縮:

ar -x lib.a
           

lipo

create or operate on universal files

lipo主要用于處理通用二進制檔案。

檢視架構資訊:

lipo -info a.framework/a
           

合并生成一個胖包:

lipo -create Debug-iphoneos/a.framework/a Debug-simulators/a.framework/a -output aout
           

分離出armv7版本庫:

lipo a.framework/a -thin armv7 -output aout
           

nm

display name list (symbol table)

展示符号資訊:

nm -nm a.out
           

otool

object file displaying tool

比nm更強大,可以詳細檢視mach-o檔案資訊。mac還有一個對應的圖形化工具——MachOView。

檢視依賴動态庫:

otool -L a.out
           

檢視反彙編代碼段:

otool -v -t a.out
           

objdump

display information from object files

反彙編目标檔案或可執行檔案。

反彙編a.out所有header資訊:

objdump -x a.out
           

反彙編a.out所有section資訊:

objdump -D a.out
           

intstall_name_tool

change dynamic shared library install names

這個工具用的比較少,它可以修改動态庫的找尋路徑。

例如cmake生成的動态庫添加到工程可能存在dyld: Library not loaded 的錯誤,這個時候,我們先用 otool 檢視一下這個動态庫的路徑:

otool -L  a.framework/a
           

得到結果:

/Users/worthy/Desktop/a.framework/a (compatibility version 0.0.0, current version 0.0.0)
    /usr/lib/libz.1.dylib (compatibility version 1.0.0, current version 1.2.8)
    /usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 307.5.0)
    /usr/lib/libSystem.dylib (compatibility version 1.0.0, current version 1238.50.2)

           

注意上面的動态庫路徑是個絕對路徑,動态連結器加載的時候肯定找不到動态庫,我們需要用@RPATH的相對路徑,需要使用install_name_tool修改:

install_name_tool -id @rpath/a.framework/a a.framework/a
           

繼續閱讀