通过Xcode命令行编译

xiaoxiao2021-02-28  97

通过Xcode命令行编译

通过Xcode命令行编译

本文档提供命令行常提到一些问题

命令行工具是什么

命令行工具包是一个小型独立包,可供下载独立于Xcode的和允许您执行命令行开发OS X,它由两部分组成:OS X SDK和命令行工具,如Clang的,这是安装在/ usr/ bin中

下载命令行工具无法使用在Xcode的OS X10.9。我怎么能在我的机器上安装它们?

在OS X10.9,Xcode预设的下载窗格将不会支持下载命令行工具,使用下列任何一个方式到你的系统上安装命令行的工具:

使用Xcode

如果您计算机上安装Xcode,那么就没有必要安装它们。Xcode绑定了所有的命令行工具。OSX10.9 包含所有的安装包,这些垫片,安装在/ usr/ bin中,可以映射列入在/ usr / bin添加到里面的Xcode相应之一的任何工具。xcrun就是这样垫片之一,它可以让你找到或者在命令行内运行的Xcode任何工具。使用它可从命令行调用在Xcode中的任何工具。

图1 在终端应用程序运行dwarfdump
$ xcrun dwarfdump --uuid MySample.app/MySample UUID: AD019F0E-1318-3F9F-92B6-9F95FBEBBE6F (armv7) MySample.app/MySample UUID: 4776671F-0C12-3172-AE97-3676E83E3F19 (armv7s) MySample.app/MySample UUID: BB59C973-06AC-388F-8EC1-FA3701C9E264 (arm64) MySample.app/MySample
用终端

如清单2中看到,您可以通过运行xcode-select --install 命令或试图使用在终端任何其他工具进行安装

尝试在终端使用Git
$ git xcode-select: note: no developer tools were found at '/Applications/Xcode.app', requesting install. Choose an option in the dialog to download the command line developer tools.

OS X还附带在Xcode-Select,一个是安装在/ usr/ bin命令行工具,它允许你管理活跃的开发者目录下Xcode和其他BSD的开发工具,有关详细信息,请参阅其手册页。

OS X会显示一个对话框,尝试运行在终端的任何工具时,如图1所示.

选择“Install”来设置命令行工具包的/Library/Developer/CommandLineTools。

图一 对话框下载工具

Paste_Image.png

在开发者网站上使用“Download for Apple Developers”页面

命令行工具包可供下载的下载地址用于Apple开发者页面,登录你的Apple ID,然后搜索并下载命令行工具包OS X Mavericks,

如图2。在命令行的工具包下载地址页面

Paste_Image.png

在OS X10.9,软件更新时会通知你的命令行工具的新版本可用于更新。

如何卸载我的命令行工具?

Xcode中包括所有的命令行工具。如果它被安装在系统上,删除卸载你的工具。如果你的工具是从Xcode中单独下载,那么他们位于/Library/Developer/CommandLineTools。删除CommandLineTools文件夹将其卸载。

我有我机器上安装的Xcode的多个版本。什么版本的Xcode确实目前我使用的命令行工具?

要找出正在使用的Xcode的版本用你的工具,请在终端中输入以下命令:

$ xcode-select --print-path

打印目前使用我的工具Xcode的版本

$ xcode-select --print-path /Applications/Xcode5.1.1/Xcode.app/Contents/Developer

如何选择Xcode中的默认版本用于我的命令行工具?

为了选择适合您的命令行工具Xcode默认运行在终端下面的命令:

$ sudo xcode-select -switch <path/to/>Xcode.app

其中,<path/to/>是路径要使用的开发Xcode.app包

设置Xcode的默认版本

$ sudo xcode-select -switch /Applications/Xcode5.1.1/Xcode.app

我怎么用命令行编译我的工程?

xcodebuild是一个命令行的工具,可以让你的工程通过projects workspaces进行编译,测试,分析,打包。他可以运行在包含一个或者多个Target的工程上面,或者在projects workspaces包含scheme上面。xcodebuild提供了几个选项,可以在Main Page看到这些。默认情况下,xcodebuild会保存和输出在Xcode的本地定义的面板里面。

请观看下面xcodebuild运用的各种用法。请确保终端运行在projects workspaces目录,然后运行下面命令。

列出所有的Target,编译你工程的配置和schemes。在命令行打印下面的命令。

xcodebuild -list -project <your_project_name>.xcodeproj

<your_project_name>是你工程的名称。

图5 列出你工程所有的信息

$ cd /Users/username/Desktop/MyApplication $ xcodebuild -list -project MyApplication.xcodeproj Information about project "MyApplication": Targets: MyMacApp MyMacAppTests MyiOSApp MyiOSAppTests Build Configurations: Debug Release If no build configuration is specified and -scheme is not passed then "Release" is used. Schemes: MyMacApp MyiOSApp

可以运行下面的命令去编译你工程一个scheme

xcodebuild -scheme <your_scheme_name> build

<your_scheme_name>是你选择特定的编译和执行的一个scheme

图6 编译MyiOSAppscheme

$ xcodebuild -scheme MyiOSApp build xcodebuild -scheme MyiOSApp === BUILD TARGET MyiOSApp OF PROJECT MyApplication WITH CONFIGURATION Debug ===

xcodebuild还有其他执行scheme的编译命令,比如build,analyze,analyze。如图7所示,没有任何的方法,xcodebuild将会使用默认的build命令。

通过configuration文件编译你的工程,运行下面的命令。

xcodebuild -target <your_target_name> -xcconfig <your_configuration_file>.xcconfig

<your_target_name>和<your_configuration_file>分别是你编译Target的名字和你配置文件的名字。

图7 通过配置文件编译MyiOSApp的target

$ xcodebuild -target MyiOSApp -xcconfig configuration.xcconfig Build settings from configuration file 'configuration.xcconfig': GCC_WARN_CHECK_SWITCH_STATEMENTS = YES GCC_WARN_UNDECLARED_SELECTOR = YES IPHONEOS_DEPLOYMENT_TARGET = 6.0 PRODUCT_NAME = MyApp === BUILD TARGET MyiOSApp OF PROJECT MyApplication WITH THE DEFAULT CONFIGURATION (Debug) ===

改变你xcodebuild本地的输出命令,运用SYMROOT(编译你工程的命令)DSTROOT(安装编译本地目录)。分别指定Debug发布,Release发布和.dSYM的本地文件路径。想知道更多的编译设置点击这里。

为MyiOSApp's 设置一个Debug版本的本地路径`

$ xcodebuild -scheme MyiOSApp SYMROOT="/Users/username/DebugLocation" Build settings from command line: SYMROOT = "/Users/username/DebugLocation" === BUILD TARGET MyiOSApp OF PROJECT MyApplication WITH CONFIGURATION Debug ===

为MyiOSApp's 设置一个Release版本的本地路径

$ xcodebuild -scheme MyiOSApp DSTROOT="/Users/username/ReleaseLocation" archive Build settings from command line: DSTROOT = /Users/username/ReleaseLocation === BUILD TARGET MyiOSApp OF PROJECT MyApplication WITH CONFIGURATION Release ===

App有很多的设置选项,我怎么为xcodebuild设置一个默认的设置

在Xcode的Info的面板设置一个弹出的菜单,当你编译Target这里设置xcodebuild编译的默认选项。通过下面可以知道怎么用弹出的菜单设置一个默认的xcodebuild选项

给xcodebuild设置默认编译Debug

Paste_Image.png

我怎么通过命令行运行unit测试的命令。

为了执行unit测试,你可以通过下面的命令。

xcodebuild test -scheme <your_scheme_name> -destination destinationspecifier

xcodebuild运用test的编译命令去执行unit测试。这个编译命令必须有一个scheme说明和明确的目的。-destination选项可以让你为unit测试做目的说明。最后-destinationspecifier的是device,simulator或者Mac的说明。它由一组用逗号分隔的key = value键值对,这都依赖于设备,所使用模拟器,或所使用Mac。

可以通过Xcode scheme或者选择一个目标来运行你的应用程序去知道scheme各自目的。

对于OS X的应用destinationspecifier支持的platform和arch所有键值看下面的表格,如果是OS X进行unit测试必须包含这两个键值

OS X支持的键值

key 描述 Value platform unit测试支持的平台 OS X arch unit测试的架构 i386 or x86_64

下面的一个例子是测试OSX APP的一个scheme。-destinationspecifier设置为'platform=OS X, arch=x86_64'

测试OS X64位MyMacAppscheme

xcodebuild test -scheme MyMacApp -destination 'platform=OS X, arch=x86_64'

对于IOS的APP,-destinationspecifier支持platform,name,id三种字段。

key 描述 Value platform unit测试支持的平台 iOS name unit测试你设备全拼 可以在Xcode的Devices Organizer找到你设备的名字 id unit测试你设备的identifier 查看你设备详细信息可以找到identifier

name和id可以platform分别组合,但是必须存在一个,可以看下面的例子

用id测试MyiOSApp scheme

xcodebuild test -scheme MyiOSApp -destination 'platform=iOS,id=998058a1c30d845d0dcec81cd6b901650a0d701c'

用name测试MyiOSApp scheme

xcodebuild test -scheme MyiOSApp -destination 'platform=iOS,name=iPod touch'

对于iOS模拟器的APP,-destinationspecifier支持platform,name,OS

下表是iOS模拟器APP支持的所有key | key | 描述 | Value | | -------- | -------------- | ---------------------------------------- | | platform | unit测试支持的平台 | iOS模拟器 | | name | unit测试模拟器的名字 | iPhone Retina (3.5-inch), iPhone Retina (4-inch), iPhone Retina (4-inch 64-bit), iPad, iPad Retina, or iPad Retina (64-bit. | | OS | iOS支持的版本 | 一个IOS版本 |

platform和name是比选的,OS是可选的。

测试MyiOSApp scheme用iPad模拟器

xcodebuild test -scheme MyiOSApp -destination 'platform=iOS Simulator,name=iPad'

测试MyiOSApp用7.1iPhone Retina (4-inch 64-bit)模拟器

xcodebuild test -scheme MyiOSApp -destination 'platform=iOS Simulator,name=iPhone Retina (4-inch 64-bit),OS=7.1'

-destination允许你在多个平台运行同一个unit测试,下面是同时添加多个平台的测试。

测试MyiOSApp用模拟器和iPod touch

xcodebuild test -scheme MyiOSApp -destination 'platform=iOS Simulator,name=iPhone Retina (4-inch 64-bit),OS=7.1' -destination 'platform=iOS,name=iPod touch'

xcodebuild会循序渐进的测试你的平台,对于上面会先测试模拟器在测试iPod touch

From: http://www.jianshu.com/p/55b80e746e38

转载请注明原文地址: https://www.6miu.com/read-49416.html

最新回复(0)