Search code examples
c++bashdebugginggccclipboard

Can I somehow filter the error message lines from GCC?


While coding in C++, I often get a really long error message for simple errors, such as mismatched parameters in function calls or parameter ambiguity. These error messages usually span through tens even hundreds of lines, and it is pretty difficult to find where the actual error is.

Is it possible to do some manipulation on the output to find the error location "lazily"?

I'm looking for something like:

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

It would be a really useful debugging tool. How do I achieve this?

Example compilation error:

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

It should output:

createClusterSizePairFrequencyValidationPlots.cc:167:62

Update to the answer of NIoSaT:

To copy the error message:

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

To copy and display only the first error:

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

Solution

  • To do this you can use grep.

    The command would be like this

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

    This does two Things

    1. It moves stderr to stdout
    2. It looks for :"any number here":"any number": error: and prints out the corresponding line

    So you should get only the lines with the information you want

    Update to the answer :

    To copy the error msg:

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

    To copy and display only the first error:

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