为啥我不能重定向 gcc -H 的输出
Posted
技术标签:
【中文标题】为啥我不能重定向 gcc -H 的输出【英文标题】:Why I can't redirect output of gcc -H为什么我不能重定向 gcc -H 的输出 【发布时间】:2016-07-14 21:48:31 【问题描述】:当我执行时
gcc -H myfile.c 2>&1 > gcc.log
我在屏幕上得到以下输出:
. /usr/include/argp.h
.. /usr/include/stdio.h
... /usr/include/features.h
.... /usr/include/x86_64-linux-gnu/sys/cdefs.h
..... /usr/include/x86_64-linux-gnu/bits/wordsize.h
.... /usr/include/x86_64-linux-gnu/gnu/stubs.h
..... /usr/include/x86_64-linux-gnu/gnu/stubs-64.h
... /usr/lib/gcc/x86_64-linux-gnu/4.8/include/stddef.h
... /usr/include/x86_64-linux-gnu/bits/types.h
.... /usr/include/x86_64-linux-gnu/bits/wordsize.h
.... /usr/include/x86_64-linux-gnu/bits/typesizes.h
... /usr/include/libio.h
.... /usr/include/_G_config.h
..... /usr/lib/gcc/x86_64-linux-gnu/4.8/include/stddef.h
..... /usr/include/wchar.h
.... /usr/lib/gcc/x86_64-linux-gnu/4.8/include/stdarg.h
... /usr/include/x86_64-linux-gnu/bits/stdio_lim.h
... /usr/include/x86_64-linux-gnu/bits/sys_errlist.h
.. /usr/include/ctype.h
... /usr/include/endian.h
.... /usr/include/x86_64-linux-gnu/bits/endian.h
.... /usr/include/x86_64-linux-gnu/bits/byteswap.h
..... /usr/include/x86_64-linux-gnu/bits/wordsize.h
..... /usr/include/x86_64-linux-gnu/bits/byteswap-16.h
... /usr/include/xlocale.h
.. /usr/include/getopt.h
.. /usr/lib/gcc/x86_64-linux-gnu/4.8/include-fixed/limits.h
... /usr/lib/gcc/x86_64-linux-gnu/4.8/include-fixed/syslimits.h
.... /usr/lib/gcc/x86_64-linux-gnu/4.8/include-fixed/limits.h
..... /usr/include/limits.h
...... /usr/include/x86_64-linux-gnu/bits/posix1_lim.h
....... /usr/include/x86_64-linux-gnu/bits/local_lim.h
........ /usr/include/linux/limits.h
...... /usr/include/x86_64-linux-gnu/bits/posix2_lim.h
.. /usr/include/errno.h
... /usr/include/x86_64-linux-gnu/bits/errno.h
.... /usr/include/linux/errno.h
..... /usr/include/x86_64-linux-gnu/asm/errno.h
...... /usr/include/asm-generic/errno.h
....... /usr/include/asm-generic/errno-base.h
. /usr/include/argz.h
.. /usr/include/errno.h
.. /usr/include/string.h
... /usr/lib/gcc/x86_64-linux-gnu/4.8/include/stddef.h
. /usr/include/stdlib.h
.. /usr/lib/gcc/x86_64-linux-gnu/4.8/include/stddef.h
.. /usr/include/x86_64-linux-gnu/bits/waitflags.h
.. /usr/include/x86_64-linux-gnu/bits/waitstatus.h
.. /usr/include/x86_64-linux-gnu/sys/types.h
... /usr/include/time.h
... /usr/lib/gcc/x86_64-linux-gnu/4.8/include/stddef.h
... /usr/include/x86_64-linux-gnu/sys/select.h
.... /usr/include/x86_64-linux-gnu/bits/select.h
..... /usr/include/x86_64-linux-gnu/bits/wordsize.h
.... /usr/include/x86_64-linux-gnu/bits/sigset.h
.... /usr/include/time.h
.... /usr/include/x86_64-linux-gnu/bits/time.h
... /usr/include/x86_64-linux-gnu/sys/sysmacros.h
... /usr/include/x86_64-linux-gnu/bits/pthreadtypes.h
.... /usr/include/x86_64-linux-gnu/bits/wordsize.h
.. /usr/include/alloca.h
... /usr/lib/gcc/x86_64-linux-gnu/4.8/include/stddef.h
.. /usr/include/x86_64-linux-gnu/bits/stdlib-float.h
.. /usr/lib/gcc/x86_64-linux-gnu/4.8/include/stdbool.h
.. /usr/lib/gcc/x86_64-linux-gnu/4.8/include/stdint.h
... /usr/include/stdint.h
.... /usr/include/x86_64-linux-gnu/bits/wchar.h
.... /usr/include/x86_64-linux-gnu/bits/wordsize.h
Multiple include guards may be useful for:
/usr/include/limits.h
/usr/include/linux/errno.h
/usr/include/wchar.h
/usr/include/x86_64-linux-gnu/asm/errno.h
/usr/include/x86_64-linux-gnu/bits/byteswap-16.h
/usr/include/x86_64-linux-gnu/bits/byteswap.h
/usr/include/x86_64-linux-gnu/bits/endian.h
/usr/include/x86_64-linux-gnu/bits/errno.h
/usr/include/x86_64-linux-gnu/bits/local_lim.h
/usr/include/x86_64-linux-gnu/bits/select.h
/usr/include/x86_64-linux-gnu/bits/sigset.h
/usr/include/x86_64-linux-gnu/bits/stdio_lim.h
/usr/include/x86_64-linux-gnu/bits/stdlib-float.h
/usr/include/x86_64-linux-gnu/bits/sys_errlist.h
/usr/include/x86_64-linux-gnu/bits/time.h
/usr/include/x86_64-linux-gnu/bits/typesizes.h
/usr/include/x86_64-linux-gnu/bits/waitflags.h
/usr/include/x86_64-linux-gnu/bits/waitstatus.h
/usr/include/x86_64-linux-gnu/gnu/stubs-64.h
/usr/include/x86_64-linux-gnu/gnu/stubs.h
/usr/lib/gcc/x86_64-linux-gnu/4.8/include-fixed/syslimits.h
当我查看 gcc.log 时,它是空的。到底是怎么回事?为什么我无法捕获gcc -H
的输出?
消息Multiple include guards may be useful for:
是什么意思?
感谢任何帮助。
【问题讨论】:
也许这不是标准错误。可能是其他一些也写入终端的文件句柄。尝试 3、4 等...而不是 2 重定向顺序错误:gcc -H myfile.c > gcc.log 2>&1
【参考方案1】:
在 OSX(使用 clang
)上,gcc -H
确实写入标准错误。但是,您的 shell 在重定向 stderr 之后重定向 stdin,因为它在处理 2>&1
之前处理 > gcc.log
。
相反,您可以颠倒重定向的顺序:
$ gcc -H myfile.c > gcc.log 2>&1
这将首先执行stdin到gcc.log
的重定向,然后将stderr重定向到stdin,现在是gcc.log
。
或者,您可以同时明确表示:
$ gcc -H myfile.c > gcc.log 2> gcc.log
【讨论】:
输出中的Multiple include guards may be useful for:
怎么样。这是什么意思?
@flashburn 据我所知,“多重包含保护”是一种使用#define
和#ifdef
来避免多次包含同一个文件的方法,以防出现不良行为。该行可能意味着这些文件被多次包含,它们应该使用包含保护。但是,我不太确定。有关更多信息,请参阅this question。
不过,在这种情况下,“多重包含守卫”与系统标头有关,您对此无能为力。如果您打开 /usr/include/limits.h 文件,它可能缺少 include guard【参考方案2】:
您以错误的顺序进行重定向,并且 AFAIK,您只需要重定向标准错误,因为标准输出上没有任何内容。
$ gcc -H myFile.c 2>gcc.log
在>gcc.log
之前执行2>&1
将首先将标准错误重定向到控制台(标准输出所在的位置),然后 将标准输出重定向到文件。
【讨论】:
以上是关于为啥我不能重定向 gcc -H 的输出的主要内容,如果未能解决你的问题,请参考以下文章