我能以某种方式过滤来自 GCC 的错误消息行吗?

Can I somehow filter the error message lines from GCC?

在使用 C++ 进行编码时,我经常会收到很长的错误消息来表示简单的错误,例如函数调用中的参数不匹配或参数不明确。这些错误信息通常跨越数十甚至数百行,很难找到真正的错误所在。

是否可以对输出进行一些操作以找到错误位置"lazily"?

我正在寻找类似的东西:

make |& xclip_error_loc
... (make output)
(prints and/or adds this to the clipboard: sourcefile.cc:86:2)

这将是一个非常有用的调试工具。我该如何实现?

示例编译错误:

Compiling test: "createClusterSizePairFrequencyValidationPlots"...
src/createClusterSizePairFrequencyValidationPlots.cc: In function 'int main(int, char**)':
src/createClusterSizePairFrequencyValidationPlots.cc:167:62: error: no matching function for call to 'TH2I::Fill(const int&, const float&, const char [2])'
 ngthYVsPhi_H  .Fill(cluster.sizeY,       phi, "1");
                                                  ^
In file included from /cvmfs/cms.cern.ch/slc6_amd64_gcc530/cms/cmssw/CMSSW_8_0_18/external/slc6_amd64_gcc530/bin/../../../../../../lcg/root/6.06.00-ikhhed4/include/TH2D.h:25:0,
                 from src/../interface/ClusterPairFunctions.h:7,
                 from src/createClusterSizePairFrequencyValidationPlots.cc:8:
/cvmfs/cms.cern.ch/slc6_amd64_gcc530/cms/cmssw/CMSSW_8_0_18/external/slc6_amd64_gcc530/bin/../../../../../../lcg/root/6.06.00-ikhhed4/include/TH2.h:63:13: note: candidate: virtual Int_t TH2::Fill(Double_t)
    Int_t    Fill(Double_t); //MayNotUse
             ^
/cvmfs/cms.cern.ch/slc6_amd64_gcc530/cms/cmssw/CMSSW_8_0_18/external/slc6_amd64_gcc530/bin/../../../../../../lcg/root/6.06.00-ikhhed4/include/TH2.h:63:13: note:   candidate expects 1 argument, 3 provided
/cvmfs/cms.cern.ch/slc6_amd64_gcc530/cms/cmssw/CMSSW_8_0_18/external/slc6_amd64_gcc530/bin/../../../../../../lcg/root/6.06.00-ikhhed4/include/TH2.h:64:13: note: candidate: virtual Int_t TH2::Fill(const char*, Double_t)
    Int_t    Fill(const char*, Double_t) { return Fi
             ^
/cvmfs/cms.cern.ch/slc6_amd64_gcc530/cms/cmssw/CMSSW_8_0_18/external/slc6_amd64_gcc530/bin/../../../../../../lcg/root/6.06.00-ikhhed4/include/TH2.h:64:13: note:   candidate expects 2 arguments, 3 provided
/cvmfs/cms.cern.ch/slc6_amd64_gcc530/cms/cmssw/CMSSW_8_0_18/external/slc6_amd64_gcc530/bin/../../../../../../lcg/root/6.06.00-ikhhed4/include/TH2.h:75:21: note: candidate: virtual Int_t TH2::Fill(Double_t, Double_t)
    virtual Int_t    Fill(Double_t x, Double_t y);
                     ^
/cvmfs/cms.cern.ch/slc6_amd64_gcc530/cms/cmssw/CMSSW_8_0_18/external/slc6_amd64_gcc530/bin/../../../../../../lcg/root/6.06.00-ikhhed4/include/TH2.h:75:21: note:   candidate expects 2 arguments, 3 provided
/cvmfs/cms.cern.ch/slc6_amd64_gcc530/cms/cmssw/CMSSW_8_0_18/external/slc6_amd64_gcc530/bin/../../../../../../lcg/root/6.06.00-ikhhed4/include/TH2.h:76:21: note: candidate: virtual Int_t TH2::Fill(Double_t, Double_t, Double_t)
    virtual Int_t    Fill(Double_t x, Double_t y, Do
                     ^
/cvmfs/cms.cern.ch/slc6_amd64_gcc530/cms/cmssw/CMSSW_8_0_18/external/slc6_amd64_gcc530/bin/../../../../../../lcg/root/6.06.00-ikhhed4/include/TH2.h:76:21: note:   no known conversion for argument 3 from 'const char [2]' to 'Double_t {aka double}'
/cvmfs/cms.cern.ch/slc6_amd64_gcc530/cms/cmssw/CMSSW_8_0_18/external/slc6_amd64_gcc530/bin/../../../../../../lcg/root/6.06.00-ikhhed4/include/TH2.h:77:21: note: candidate: virtual Int_t TH2::Fill(Double_t, const char*, Double_t)
    virtual Int_t    Fill(Double_t x, const char *na
                     ^
/cvmfs/cms.cern.ch/slc6_amd64_gcc530/cms/cmssw/CMSSW_8_0_18/external/slc6_amd64_gcc530/bin/../../../../../../lcg/root/6.06.00-ikhhed4/include/TH2.h:77:21: note:   no known conversion for argument 2 from 'const float' to 'const char*'
/cvmfs/cms.cern.ch/slc6_amd64_gcc530/cms/cmssw/CMSSW_8_0_18/external/slc6_amd64_gcc530/bin/../../../../../../lcg/root/6.06.00-ikhhed4/include/TH2.h:78:21: note: candidate: virtual Int_t TH2::Fill(const char*, Double_t, Double_t)
    virtual Int_t    Fill(const char *namex, Double_
                     ^
/cvmfs/cms.cern.ch/slc6_amd64_gcc530/cms/cmssw/CMSSW_8_0_18/external/slc6_amd64_gcc530/bin/../../../../../../lcg/root/6.06.00-ikhhed4/include/TH2.h:78:21: note:   no known conversion for argument 3 from 'const char [2]' to 'Double_t {aka double}'
/cvmfs/cms.cern.ch/slc6_amd64_gcc530/cms/cmssw/CMSSW_8_0_18/external/slc6_amd64_gcc530/bin/../../../../../../lcg/root/6.06.00-ikhhed4/include/TH2.h:79:21: note: candidate: virtual Int_t TH2::Fill(const char*, const char*, Double_t)
    virtual Int_t    Fill(const char *namex, const c
                     ^
(lots of similar lines...)

它应该输出:

createClusterSizePairFrequencyValidationPlots.cc:167:62

更新到NIoSaT的回答:

复制错误信息:

make 2>&1 | egrep -o ":[0-9]+:[0-9]+: error" | xargs echo -n | xclip -selection clipboard && xclip -selection clipboard -o && echo ""

要复制并仅显示第一个错误:

make 2>&1 | egrep -o ":[0-9]+:[0-9]+: error" | head -n 1 | xargs echo -n | xclip -selection clipboard && xclip -selection clipboard -o && echo ""

为此,您可以使用 grep。

命令是这样的

make 2>&1 | egrep ":[0-9]+:[0-9]+: error:"

这有两件事

  1. 它将 stderr 移动到 stdout
  2. 它查找 :"any number here":"any number": error: 并打印出相应的行

所以你应该只得到包含你想要的信息的行

更新答案:

复制错误消息:

make 2>&1 | egrep -o ":[0-9]+:[0-9]+: error" | xargs echo -n | xclip -selection clipboard && xclip -selection clipboard -o && echo ""

只复制并显示第一个错误:

make 2>&1 | egrep -o ":[0-9]+:[0-9]+: error" | head -n 1 | xargs echo -n | xclip -selection clipboard && xclip -selection clipboard -o && echo ""