Table of Contents
Question content
Solution
Home Backend Development Golang Use go build to build static libraries for the iPhone simulator

Use go build to build static libraries for the iPhone simulator

Feb 09, 2024 pm 10:33 PM
go language iphone emulator ios16

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

php editor Zimo introduced: When developing iOS applications, we often need to use static libraries to extend functions or provide some common tool functions. Developers who develop using the Go language may wonder how to use the go build command to build a static library for the iPhone simulator. In this article, we will introduce in detail how to use the go build command to build a static library, and provide some practical tips and precautions to help developers successfully complete the static library building process. Whether you are a beginner or an experienced developer, you can read this article to gain practical knowledge and tips on building static libraries.

Question content

I use the following method to build a c archive in my ios project:

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

clangwrap.sh looks like this

#!/bin/sh

# go/clangwrap.sh

sdk_path=`xcrun --sdk $sdk --show-sdk-path`
clang=`xcrun --sdk $sdk --find clang`

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

exec $clang -arch $carch -isysroot $sdk_path -mios-version-min=10.0 "$@"
Copy after login

When I link it in xcode and try to run it using the simulator, I can only run it on the device itself:

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

How to position the simulator of go build as a static library used in a swift project?

Solution

Requirements

  • Create a static library for the iphone simulator
  • Use apple silicon instead of intel emulator
  • Ability to achieve a specific minimum version

tl;dr

If you select the simulator as the run destination, you can do something similar to xcode.

So basically use something like -target arm64-apple-ios16.2-simulator instead of -arch arm64 . Also omit -mios-version-min=10.0, since the actual minimum version is encoded in -target (e.g. 16.2), it takes precedence (the correct option for the emulator is anyway -miphonesimulator-version-min) .

Then, as cgo_ldflags, also specify the -target option as well as -syslibroot and the path to the sdk.

With a slight tweak to your build script, it might look like this:

This specifies the emulator as the target, with a minimum version of 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
Copy after login

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
Copy after login

clangwrap.sh

Then clangwrap.sh simplifies to:

#!/bin/zsh

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

exec "$clang" -target "$target" -isysroot "$sdk_path" "$@"
Copy after login

details

Different sdk

Must specify different sdk for ios device and iphone simulator. You can find them next to other platforms supported by xcode Under /applications/xcode.app/contents/developer/platforms. For example, in xcode 14.2 etc., there is a iphoneos platform with iphoneos16.2.sdk and a with iphonesimulator16.2.sdk iphonesimulator platform.

An apple employee in the apple developer forum posted this interesting post: https://developer.apple.com/forums/thread/673387#662260022

To inspect the generated static library for the load command, you can call:

otool -l libuplink.a
Copy after login

The generated static library for the apple silicon simulator should display the following:

...
load command 1
      cmd lc_build_version
  cmdsize 24
 platform 7
    minos 15.0
      sdk 16.2
...
Copy after login

Note: platform 7 represents the simulator, minos represents the minimum deployment target, and sdk represents the actual sdk version used.

See the section in the include file loader.h which reads:

/* 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
Copy after login

You can view them yourself on your system as follows:

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

Built specifically for iphone devices

To build a static library for the iphone sdk you need to change the following:

export sdk=iphoneos
Copy after login

In the build.sh script above.

otool -l The output will appear as:

...
Load command 1
      cmd LC_BUILD_VERSION
  cmdsize 24
 platform 2
    minos 15.0
      sdk 16.2
   ntools 0
...
Copy after login

Note: platform 2 stands for platform_ios and not the emulator.

This of course works perfectly on the device.

The above is the detailed content of Use go build to build static libraries for the iPhone simulator. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1261
29
C# Tutorial
1234
24
How to solve the user_id type conversion problem when using Redis Stream to implement message queues in Go language? How to solve the user_id type conversion problem when using Redis Stream to implement message queues in Go language? Apr 02, 2025 pm 04:54 PM

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

Multi-party certification: iPhone 17 standard version will support high refresh rate! For the first time in history! Multi-party certification: iPhone 17 standard version will support high refresh rate! For the first time in history! Apr 13, 2025 pm 11:15 PM

Apple's iPhone 17 may usher in a major upgrade to cope with the impact of strong competitors such as Huawei and Xiaomi in China. According to the digital blogger @Digital Chat Station, the standard version of iPhone 17 is expected to be equipped with a high refresh rate screen for the first time, significantly improving the user experience. This move marks the fact that Apple has finally delegated high refresh rate technology to the standard version after five years. At present, the iPhone 16 is the only flagship phone with a 60Hz screen in the 6,000 yuan price range, and it seems a bit behind. Although the standard version of the iPhone 17 will have a high refresh rate screen, there are still differences compared to the Pro version, such as the bezel design still does not achieve the ultra-narrow bezel effect of the Pro version. What is more worth noting is that the iPhone 17 Pro series will adopt a brand new and more

What is the problem with Queue thread in Go's crawler Colly? What is the problem with Queue thread in Go's crawler Colly? Apr 02, 2025 pm 02:09 PM

Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

What should I do if the custom structure labels in GoLand are not displayed? What should I do if the custom structure labels in GoLand are not displayed? Apr 02, 2025 pm 05:09 PM

What should I do if the custom structure labels in GoLand are not displayed? When using GoLand for Go language development, many developers will encounter custom structure tags...

How to view XML on your phone without a network How to view XML on your phone without a network Apr 02, 2025 pm 10:30 PM

There are two ways to view XML files: Android phones: use file manager or third-party applications (XML Viewer, DroidEdit). iPhone: Transfer files via iCloud Drive and use the Files app or third-party app (XML Buddha, Textastic).

In Go, why does printing strings with Println and string() functions have different effects? In Go, why does printing strings with Println and string() functions have different effects? Apr 02, 2025 pm 02:03 PM

The difference between string printing in Go language: The difference in the effect of using Println and string() functions is in Go...

What libraries are used for floating point number operations in Go? What libraries are used for floating point number operations in Go? Apr 02, 2025 pm 02:06 PM

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

How to open XML files with iPhone How to open XML files with iPhone Apr 02, 2025 pm 11:00 PM

There is no built-in XML viewer on iPhone, and you can use third-party applications to open XML files, such as XML Viewer, JSON Viewer. Method: 1. Download and install the XML viewer in the App Store; 2. Find the XML file on the iPhone; 3. Press and hold the XML file to select "Share"; 4. Select the installed XML viewer app; 5. The XML file will open in the app. Note: 1. Make sure the XML viewer is compatible with the iPhone iOS version; 2. Be careful about case sensitivity when entering file paths; 3. Be careful with XML documents containing external entities

See all articles