如何更改 Xcode 中构建线程的数量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4346150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:58:08  来源:igfitidea点击:

How to change the amount of building threads in Xcode?

c++multithreadingxcodecompilationbuilding

提问by Dries Staelens

I'm building a couple of C++ files in xcode that take a lot of memory to compile (+1 GB / file). Because I do this on my dual core laptop, xcode uses 2 threads for building. The two threads will eventually be building the files that take a lot of memory simultaneously so the system suffers memory starvation and the compilation grinds to a near halt.

我正在 xcode 中构建几个 C++ 文件,这些文件需要大量内存来编译(+1 GB/文件)。因为我在双核笔记本电脑上执行此操作,所以 xcode 使用 2 个线程进行构建。这两个线程最终将同时构建占用大量内存的文件,因此系统会遭受内存不足,编译几乎停止。

A sufficient solution for me would be to force Xcode to use only one build thread. Does anybody know a way to change how many build threads Xcode uses?

对我来说,一个足够的解决方案是强制 Xcode 只使用一个构建线程。有人知道改变 Xcode 使用多少构建线程的方法吗?

For those who are interested, the C++ files contain a sizable boost::spirit::qi parser.

对于那些感兴趣的人,C++ 文件包含一个相当大的 boost::spirit::qi 解析器。

采纳答案by Dries Staelens

The number of threads Xcode is using to perform tasks is controlled by PBXNumberOfParallelBuildSubtasksoption. You can change it with the following command: - defaults write com.apple.Xcode <key> <value>. For example:

Xcode 用于执行任务的线程数由PBXNumberOfParallelBuildSubtasks选项控制。您可以使用以下命令更改它: - defaults write com.apple.Xcode <key> <value>。例如:

defaults write com.apple.Xcode PBXNumberOfParallelBuildSubtasks 8

See Xcode User Defaultsfor more details.

有关更多详细信息,请参阅Xcode 用户默认设置

There are also many other ways to speed up a compilation, from precompiled headers to distributed builds. Read Reducing Build Timesfor more information on this.

还有许多其他方法可以加快编译速度,从预编译头文件到分布式构建。阅读减少构建时间以获取更多信息。

Good luck!

祝你好运!

回答by Jifeng Zhang

With XCode 5, you can use -parallelizeTargetsand -jobs NUMBERwith xcodebuild. According to xcodebuild --help:

在Xcode 5,你可以使用-parallelizeTargets-jobs NUMBER使用xcodebuild。根据xcodebuild --help

-parallelizeTargets     build independent targets in parallel
-jobs NUMBER            specify the maximum number of concurrent build operations
-parallelizeTargets     build independent targets in parallel
-jobs NUMBER            specify the maximum number of concurrent build operations

回答by Mimika Oh

For Xcode 4 you must set the IDEBuildOperationMaxNumberOfConcurrentCompileTasks user default, for example:

对于 Xcode 4,您必须设置 IDEBuildOperationMaxNumberOfConcurrentCompileTasks 用户默认值,例如:

defaults write com.apple.dt.Xcode IDEBuildOperationMaxNumberOfConcurrentCompileTasks 4

Note the "dt". This won't affect xcodebuild on the command line. To do that, use something like

注意“dt”。这不会影响命令行上的 xcodebuild。为此,请使用类似

xcodebuild -IDEBuildOperationMaxNumberOfConcurrentCompileTasks=4 ...

(See http://lists.apple.com/archives/xcode-users/2011/Apr/msg00403.htmland http://lists.apple.com/archives/xcode-users/2011/Jul//msg00377.html)

(参见http://lists.apple.com/archives/xcode-users/2011/Apr/msg00403.htmlhttp://lists.apple.com/archives/xcode-users/2011/Jul//msg00377.html)

回答by Jeremy W. Sherman

A single build task should never do the same work twice, and certainly not simultaneously! Factor out the massive chunk of common code into a static library so it can be recompiled only when it changes. Set a target dependency in your application on the static library and link in the static library product. Changes to the rest of your application will then no longer require rebuilding the static library, which should speed up build times tremendously.

一个构建任务永远不应该两次做同样的工作,当然不能同时做!将大量的公共代码分解成一个静态库,这样只有在它发生变化时才能重新编译。在静态库上的应用程序中设置目标依赖项,并在静态库产品中进行链接。对应用程序其余部分的更改将不再需要重新构建静态库,这将极大地加快构建时间。

Try to exhaust all project-level solutions before manipulating Xcode as a whole. It is too easy to cripple Xcode to using only a single thread and forget to change it back when you move on to a new project. The Xcode User Default Referencedocuments many options that are not exposed via the Preferences interface, including:

在整体操作 Xcode 之前尝试穷尽所有项目级解决方案。很容易让 Xcode 瘫痪到只使用一个线程,而在你转移到一个新项目时忘记把它改回来。在Xcode的用户默认参考文档,这些文档不通过预置接口,包括裸露的许多选项:

  • PBXNumberOfParallelBuildSubtasks(positive integer)

    This allows you to limit Xcode to using only nbuild threads on every project it compiles.

  • BuildSystemCacheSizeInMegabytes(positive integer, default 1024)

  • BuildSystemCacheMinimumRemovalAgeInHours(positive integer, default 24)

    Upping the PCH cache size and retention time could help speed up your builds.

  • PBXNumberOfParallelBuildSubtasks(正整数)

    这允许您限制 Xcode在它编译的每个项目上仅使用n 个构建线程。

  • BuildSystemCacheSizeInMegabytes(正整数,默认 1024)

  • BuildSystemCacheMinimumRemovalAgeInHours(正整数,默认 24)

    增加 PCH 缓存大小和保留时间有助于加快构建速度。