gpt4 book ai didi

swift:如何从命令行生成 arm7 或 arm64

转载 作者:IT王子 更新时间:2023-10-29 05:36:21 25 4
gpt4 key购买 nike

使用 swiftc 的 Xcode 8.3 beta 版本, 如何生成 Arm7 或 Arm64 二进制文件?

我试过明显的论点 -target-cpu arm64这给了我一个链接器消息 <unknown>:0: warning: argument unused during compilation: '-mcpu=arm64'
ld: library not found for -lobjc
并继续努力构建 x64 目标。

实际命令:

swiftc -sdk /Applications/Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/ -L /Applications/Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/lib -F /Applications/Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/System/Library/Frameworks/ -swift-version 3 -target-cpu arm64 somefile.swift

最佳答案

您可以使用 sdk 和 target 选项来执行此操作。这是一个例子:

/Projects/Test $ cat main.swift 
print("Hello world!");

为 x86_64 编译

/Projects/Test $ swiftc main.swift 
/Projects/Test $ lipo -info main
Non-fat file: main is architecture: x86_64
/Projects/Test $ ./main
Hello world!

为armv7编译

/Projects/Test $ swiftc main.swift -sdk /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS10.2.sdk -target armv7-apple-ios8.1
/Projects/Test $ lipo -info main
Non-fat file: main is architecture: armv7

为 arm64 编译

/Projects/Test $ swiftc main.swift -sdk /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS10.2.sdk -target arm64-apple-ios8.1
/Projects/Test $ lipo -info main
Non-fat file: main is architecture: arm64

我写了一个脚本 build.sh 以便于使用:

#!/bin/sh
TARGET_MAC_VERSION=10.11
TARGET_IOS_VERSION=8.1

if [ "$#" -ne 2 ]; then
echo "Usage $0: <armv7/arm64/x86_64> <file>"
exit
fi

if [ "$1" != 'armv7' ] && [ "$1" != 'arm64' ] && [ "$1" != 'x86_64' ]; then
echo "Usage $0: <armv7/arm64/x86_64>"
exit
fi

if [ "$1" == 'x86_64' ]; then
SDK=macosx
TARGET="x86_64-macosx$TARGET_MAC_VERSION"
else
SDK=iphoneos
TARGET="$1-apple-ios$TARGET_IOS_VERSION"
fi
echo "xcrun -sdk $SDK swiftc $2 -target $TARGET"
xcrun -sdk $SDK swiftc $2 -target $TARGET

输出

  $ ./build.sh armv7 main.swift 
xcrun -sdk iphoneos swiftc main.swift -target armv7-apple-ios8.1
$ lipo -info main
Non-fat file: main is architecture: armv7
$ ./build.sh arm64 main.swift
xcrun -sdk iphoneos swiftc main.swift -target arm64-apple-ios8.1
$ lipo -info main
Non-fat file: main is architecture: arm64
$ ./build.sh x86_64 main.swift
xcrun -sdk macosx swiftc main.swift -target x86_64-macosx10.11
$ lipo -info main
Non-fat file: main is architecture: x86_64

编辑说明:根据@jens 的输入优化了脚本。请参阅评论以获取更多信息。

关于swift:如何从命令行生成 arm7 或 arm64,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42677093/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com