Shell重定向:scons调用gcc,构建错误重定向不起作用

Shell redirect: scons calls gcc, build error redirection doesn't work

我正在使用调用 gcc 构建文件的 scons(python 构建工具),如下所示:

$scons
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
g++ -o 1.o -c 1.cpp
1.cpp:20:5: error: no matching function for call to 'g'
    g(&obj2);
    ^
1.cpp:12:6: note: candidate function not viable: no known conversion from 'B *' to 'A &' for 1st argument; remove &
void g(A&a){
     ^
1 error generated.
scons: *** [1.o] Error 1
scons: building terminated because of errors.

然后我尝试将所有输出保存到一个文件中。我想错误消息在 stderr 中,所以我尝试将 fd=2 重定向到 fd=1,如下所示:

$scons 2>&1 >error1
1.cpp:20:5: error: no matching function for call to 'g'
    g(&obj2);
    ^
1.cpp:12:6: note: candidate function not viable: no known conversion from 'B *' to 'A &' for 1st argument; remove &
void g(A&a){
     ^
1 error generated.
scons: *** [1.o] Error 1

但似乎error1 只包含"scons" 命令本身的信息。所有 gcc 错误消息仍在屏幕上,未保存在 "error1"

$cat error1
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
g++ -o 1.o -c 1.cpp
scons: building terminated because of errors.

那么如何让所有称为程序的 scon 将它们的 fd=2 重新定向到 fd=1?或者这是 shell 重定向的限制,只能重定向顶部调用者的流?

执行重定向 left-to-right,指的是截至前一个执行完成时目的地的状态。

  • 2>&1 >error1按以下操作顺序执行:

    1. FD 2 指向操作开始时 FD 1 指向的目标(因为您报告内容已写入屏幕,这大概是您的终端)。

    2. FD 1 指向文件error1.

  • >error1 2>&1按以下操作顺序执行:

    1. FD 1指向文件error1

    2. FD 2 指向 FD 1 当前指向的位置(因此, 文件 error1)。


因此,在 2>&1 >error1 的情况下,只有 FD 1 (stdout) 而不是 FD 2 (stderr) 指向 error1,因为 2>&1 是调用时,FD 1 指向终端。