【问题标题】:Shell redirect: scons calls gcc, build error redirection doesn't workShell 重定向:scons 调用 gcc,构建错误重定向不起作用
【发布时间】:2017-02-17 09:57:03
【问题描述】:

我使用的是 scons(python build tool),它调用 gcc 来构建文件,如下所示:

$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.

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

【问题讨论】:

标签: shell redirect gcc stdout stderr


【解决方案1】:

重定向从左到右执行,指的是在前一个完成执行时其目的地的状态。

  • 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 指向终端。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-08
    • 1970-01-01
    • 1970-01-01
    • 2018-02-18
    相关资源
    最近更新 更多