Linux 如何将编译器的所有输出捕获到文件中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2297536/
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-08-03 19:47:19  来源:igfitidea点击:

How do I capture all of my compiler's output to a file?

linuxshellg++makefileio-redirection

提问by pecker

I'm building an opensource project from source (CPP) in Linux. This is the order:

我正在 Linux 中从源代码 (CPP) 构建一个开源项目。这是顺序:

$CFLAGS="-g Wall" CXXFLAGS="-g Wall" ../trunk/configure --prefix=/somepath/ --host=i386-pc --target=i386-pc
$make

While compiling I'm getting lot of compiler warnings. I want to start fixing them. My question is how to capture all the compiler output in a file?

在编译时,我收到了很多编译器警告。我想开始修理它们。我的问题是如何捕获文件中的所有编译器输出?

$make > fileis not doing the job. It's just saving the compiler command like g++ -someoptions /asdf/xyz.cppI want the output of these command executions.

$make > file不是在做这项工作。它只是保存编译器命令,就像g++ -someoptions /asdf/xyz.cpp我想要这些命令执行的输出一样。

回答by David Thornley

Try make 2> file. Compiler warnings come out on the standard error stream, not the standard output stream. If my suggestion doesn't work, check your shell manual for how to divert standard error.

试试make 2> file。编译器警告出现在标准错误流上,而不是标准输出流上。如果我的建议不起作用,请查看您的 shell 手册以了解如何转移标准错误。

回答by Nathan Kidd

In a bourne shell:

在伯恩壳中:

make > my.log 2>&1

制作> my.log 2>&1

I.e. > redirects stdout, 2>&1 redirects stderr to the same place as stdout

即 > 重定向标准输出,2>&1 将标准错误重定向到与标准输出相同的位置

回答by kennytm

The output went to stderr. Use 2>to capture that.

输出到 stderr。使用2>捕捉到这一点。

$make 2> file

回答by John Feminella

The compiler warnings happen on stderr, not stdout, which is why you don't see them when you just redirect makesomewhere else. Instead, try this if you're using Bash:

编译器警告发生在stderr,而不是stdout,这就是为什么当您重定向make到其他地方时看不到它们的原因。相反,如果您使用 Bash,请尝试以下操作:

$ make &> results.txt

The &means "redirect stdout and stderr to this location". Other shells often have similar constructs.

&意思是“重定向输出和错误到这个位置”。其他壳通常具有类似的构造。

回答by dmckee --- ex-moderator kitten

Lots of good answers so far. Here's a frill:

到目前为止有很多好的答案。这是一个装饰:

$ make 2>&1 | tee filetokeepitin.txt 

will let you watch the output scroll past.

会让你看到输出滚动过去。

回答by Gauthier

From http://www.oreillynet.com/linux/cmd/cmd.csp?path=g/gcc

来自http://www.oreillynet.com/linux/cmd/cmd.csp?path=g/gcc

The > character does not redirect the standard error. It's useful when you want to save legitimate output without mucking up a file with error messages. But what if the error messages are what you want to save? This is quite common during troubleshooting. The solution is to use a greater-than sign followed by an ampersand. (This construct works in almost every modern UNIX shell.) It redirects both the standard output and the standard error. For instance:

$ gcc invinitjig.c >& error-msg

> 字符不会重定向标准错误。当您想保存合法输出而不破坏带有错误消息的文件时,它很有用。但是,如果错误消息是您想要保存的内容呢?这在故障排除过程中很常见。解决方案是使用一个大于号,后跟一个与号。(这个结构几乎适用于所有现代 UNIX shell。)它重定向标准输出和标准错误。例如:

$ gcc invinitjig.c >& error-msg

Have a look there, if this helps: another forum

看看那里,如果这有帮助: 另一个论坛

回答by Peter Nguyen

Assume you want to hilight warning and error from build ouput:

假设您想从构建输出中突出警告和错误:

make |& grep -E "warning|error"

回答by Immanuel Litzroth

It is typically not what you want to do. You want to run your compilation in an editor that has support for reading the output of the compiler and going to the file/line char that has the problems. It works in all editors worth considering. Here is the emacs setup:

这通常不是您想要做的。您希望在支持读取编译器输出并转到有问题的文件/行字符的编辑器中运行编译。它适用于所有值得考虑的编辑器。这是emacs设置:

https://www.gnu.org/software/emacs/manual/html_node/emacs/Compilation.html

https://www.gnu.org/software/emacs/manual/html_node/emacs/Compilation.html

回答by SunDontShine

In C shell- The ampersand is after the greater-than symbol

C shell 中- &符号在大于号之后

make >& filename

回答by easwaran

Based on an earlier reply by @dmckee

基于@dmckee 较早的回复

make | tee makelog.txt

This gives you real-time scrolling output while compiling, and simultaneously write to the makelog.txt file.

这在编译时为您提供实时滚动输出,并同时写入 makelog.txt 文件。