如何使xcodebuild打印编译错误和警告到stderr?

问题描述

好像xcodebuild将所有内容都打印到stdout。

$ /Applications/Xcode.app/Contents/Developer/usr/bin/xcodebuild -project test.xcodeproj build -target test -configuration Debug -jobs 3 2>err
 # xcodebuild stdout with bunch of warnings and errors

$ cat err
** BUILD Failed **


The following build commands Failed:
    CompileC _build/test.build/Objects-normal/x86_64/test.o /Users/me/test/test.cpp normal x86_64 objective-c++ com.apple.compilers.llvm.clang.1_0.compiler
(1 failure)

那不能让我的IDE(QtCreator)正确解析输出

实际上,clang将错误和警告打印到stderr。但是xcodebuild出于某种原因将所有内容重定向到strdout。除了1>&2之外,是否有办法使xcodebuild向stderr打印错误和警告?

解决方法

您可以尝试使用-quiet选项运行命令,如下所示。

# Do not print any output except for warnings and errors
/Applications/Xcode.app/Contents/Developer/usr/bin/xcodebuild -project -quiet

因此,您可以仅使用-quiet选项传递警告和错误,并且可以仅使用 2> ./errors.log

过滤错误。
/Applications/Xcode.app/Contents/Developer/usr/bin/xcodebuild -project -quiet test.xcodeproj build -target test -configuration Debug -jobs 3 2> ./errors.log

还有其他检查工具,例如 xcpretty xcbeautify

https://github.com/xcpretty/xcpretty(我喜欢这个)