【问题标题】:Redirect the output of g++ compiler to a file?将g ++编译器的输出重定向到文件?
【发布时间】:2020-08-21 03:20:39
【问题描述】:

我在编译 c++ 程序时看到以下消息。

 test.cpp: In function ‘int main()’:
 test.cpp:950: error: no match for ‘operator<<’ in ‘std::cout << d->Derived::print()’
 /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:108: note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>& (*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
 /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:117: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ios<_CharT, _Traits>& (*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]

我在想是否可以将所有这些消息重定向到一个文件并且在控制台上看不到任何内容。我尝试通过

重定向stderr消息
g++ test.cpp 2> xx

但这似乎不起作用。我仍然可以在控制台上看到所有内容,但文件中什么也没有。

【问题讨论】:

  • 忽略你这样做的理由,你试过g++ test.cpp &gt; xx吗? g++ 有可能将这些消息写入标准输出。或者您可以使用霰弹枪方法g++ test.cpp &amp;&gt; xx
  • 如果您想隐藏它们,请说明您是否要将消息重定向到存储它们。
  • 您使用哪种外壳? echo $SHELL 说什么?
  • @TedLyngmo,我明白了。你说的对。这是我看到的:echo $0 tcsh echo $SHELL /bin/bash

标签: linux g++ tcsh


【解决方案1】:

对于 csh 类型的 shell,请执行以下操作:

g++ test.cpp &gt;&amp; xx

【讨论】:

  • 谢谢特德。所以,这是重定向一切的霰弹枪方法。不管它是stderr、stdout还是其他什么,控制台上的任何东西都会转到xx。我的理解正确吗?
  • @HemantBhargava 欢迎您!它重定向stdoutstderr。如果程序以某种方式向您的终端打开另一个流并打印到该流,则不会被捕获,但这种情况极为罕见。
  • 出于好奇,如何在那个 shell 中只重定向 stderr?
  • @NO_NAME 这是一个永恒的问题 :-) 必须求助于巫术。这是一种方法:Redirecting stderr in csh 我自己从不使用csh,这是原因之一。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-04
  • 2016-08-09
  • 2013-10-11
  • 2023-03-19
  • 2014-03-31
  • 1970-01-01
相关资源
最近更新 更多