首頁  >  文章  >  後端開發  >  使用 go build 為 Iphone 模擬器建立靜態庫

使用 go build 為 Iphone 模擬器建立靜態庫

PHPz
PHPz轉載
2024-02-09 22:33:08956瀏覽

使用 go build 为 Iphone 模拟器构建静态库

php小編子墨介紹:在開發iOS應用程式時,我們常常需要使用靜態函式庫來擴充功能或提供一些通用的工具函數。使用go語言進行開發的開發者可能會想知道如何使用go build指令來建立適用於iPhone模擬器的靜態函式庫。在本文中,我們將詳細介紹如何使用go build指令來建立靜態函式庫,並且提供一些實用的技巧和注意事項,幫助開發者順利完成靜態庫的建置過程。無論你是初學者還是有一定經驗的開發者,都可以透過閱讀本文來獲得關於建立靜態庫的實用知識和技巧。

問題內容

我使用以下方法在我的 ios 專案中建立一個 c 存檔:

goos=ios goarch=arm64 cgo_enabled=1 sdk=iphonesimulator cgo_cflags="-fembed-bitcode" cc=pwd/clangwrap.sh go build -buildmode=c-archive -/clangwrap.sh go build -buildmode=c-archive -

/clangwrap.sh go build -buildmode=c-archive -

/clangwrap.sh go build -buildmode=c-archive -

1 .a

#clangwrap.sh 看起來像這樣 <pre class="brush:php;toolbar:false;">#!/bin/sh # go/clangwrap.sh sdk_path=`xcrun --sdk $sdk --show-sdk-path` clang=`xcrun --sdk $sdk --find clang` if [ &quot;$goarch&quot; == &quot;amd64&quot; ]; then carch=&quot;x86_64&quot; elif [ &quot;$goarch&quot; == &quot;arm64&quot; ]; then carch=&quot;arm64&quot; fi exec $clang -arch $carch -isysroot $sdk_path -mios-version-min=10.0 &quot;$@&quot;</pre> 當我在 xcode 中連結它並嘗試使用模擬器運行時,我只能在設備本身上運行它:

building for iOS Simulator, but linking in object file built for iOS ... for architecture arm64

如何將

go build

的模擬器定位為 swift 專案中使用的靜態函式庫? 解決方法

  • 要求
  • #為 iphone 模擬器建立靜態函式庫
使用 apple silicon 而非 intel 模擬器

能夠實現特定的最低版本

tl;dr

#如果您選擇模擬器作為運行目的地,您可以執行類似 xcode 的操作。 所以基本上是使用 -target arm64-apple-ios16.2-simulator 之類的東西而不是 -arch arm64 。也要省略

-mios-version-min=10.0

,因為實際的最小版本是在-target 中編碼的(例如16.2),它優先(模擬器的正確選項無論如何都是 -miphonesimulator-version-min) . 然後,作為 cgo_ldflags,也指定

-target

選項以及

-syslibroot

以及 sdk 的路徑。

你的建置腳本稍微調整了一下,它可能看起來像這樣: 這指定模擬器為目標,最低版本為 15。

build.sh

#
#!/bin/sh
export goos=ios
export goarch=arm64
export cgo_enabled=1
export sdk=iphonesimulator
export cgo_cflags="-fembed-bitcode"
export min_version=15

. ./target.sh

export cgo_ldflags="-target ${target} -syslibroot \"${sdk_path}\""
cc="$(pwd)/clangwrap.sh"
export cc

go build -buildmode=c-archive -o libuplink.a

target.sh

#
#!/bin/sh

sdk_path=$(xcrun --sdk "$sdk" --show-sdk-path)
export sdk_path

if [ "$goarch" = "amd64" ]; then
    carch="x86_64"
elif [ "$goarch" = "arm64" ]; then
    carch="arm64"
fi

if [ "$sdk" = "iphoneos" ]; then
  export target="$carch-apple-ios$min_version"
elif [ "$sdk" = "iphonesimulator" ]; then
  export target="$carch-apple-ios$min_version-simulator"
fi

clangwrap.sh

然後 clangwrap.sh 簡化為:

#!/bin/zsh

clang=$(xcrun --sdk "$sdk" --find clang)

exec "$clang" -target "$target" -isysroot "$sdk_path" "$@"

詳細資訊

不同的 sdk#必須為 ios 裝置和 iphone 模擬器指定不同的 sdk。您可以在 xcode 支援的其他平台旁邊找到它們 在 /applications/xcode.app/contents/developer/platforms 下。例如,在xcode 14.2 等中,有一個帶有iphoneos16.2.sdkiphoneos 平台和帶有

iphonesimulator16.2.sdk

iphonesimulator 平台。

apple 開發者論壇中的一位 apple 員工發布了這篇有趣的帖子:https: //developer.apple.com/forums/thread/673387#662260022

#要檢查產生的靜態函式庫以顯示

load

指令,可以呼叫: <pre class="brush:php;toolbar:false;">otool -l libuplink.a</pre> 產生的用於 apple silicon 模擬器的靜態庫應顯示如下內容: <pre class="brush:php;toolbar:false;">... load command 1 cmd lc_build_version cmdsize 24 platform 7 minos 15.0 sdk 16.2 ...</pre> 注意:platform 7 表示模擬器,

minos

表示最低部署目標,sdk 表示實際使用的 sdk 版本。

請參閱包含檔案

loader.h

中的部分,內容如下:

/* known values for the above platform field. */
#define platform_unknown 0
#define platform_any 0xffffff
#define platform_macos 1
#define platform_ios 2
#define platform_tvos 3
#define platform_watchos 4
#define platform_bridgeos 5
#define platform_maccatalyst 6
#define platform_iossimulator 7
#define platform_tvossimulator 8
#define platform_watchossimulator 9
#define platform_driverkit 10
您可以在自己的系統上自行查看它們,如下所示:

cat `xcrun --sdk iphonesimulator --show-sdk-path`/usr/include/mach-o/loader.h

專為 iphone 裝置建置

要為 iphone sdk 建立靜態庫,您需要更改以下內容:

export sdk=iphoneos
在上面的

build.sh 腳本中。

otool -l 的輸出將顯示為: <pre class="brush:php;toolbar:false;">... Load command 1 cmd LC_BUILD_VERSION cmdsize 24 platform 2 minos 15.0 sdk 16.2 ntools 0 ...</pre> 注意:

platform 2

代表

platform_ios### 而不是模擬器。 ### ###這當然可以在設備上完美運作。 ###

以上是使用 go build 為 Iphone 模擬器建立靜態庫的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:stackoverflow.com。如有侵權,請聯絡admin@php.cn刪除