C++ – gcov is not generating coverage information for header files

cgccgcovqt

I'm using gcov for the first time and I'm having a problem which is similar to the one reported in this thread. However, I wasn't able to solve my problem following the comments in that thread.

I'm trying to measure the coverage of a concrete test case in KMyMoney-4.6.4, namely "testConstructor" in the test file "mymoneyaccounttest.cpp". This test case is using the following method, which is in the header file "mymoneyaccount.h" and has executable code:

 const QString& institutionId(void) const {
    return m_institution;
  }

So, I build the program and the tests, and then execute:
gcov mymoneyaccount.cpp

Among the coverage information displayed, I obtain:


File 'mymoneyaccount.cpp'
Lines executed:8.91% of 393
Creating 'mymoneyaccount.cpp.gcov'

File 'mymoneyaccount.h'
Lines executed:25.00% of 4
Creating 'mymoneyaccount.h.gcov'

The coverage information in "mymoneyaccount.cpp.gcov" is ok. In contrast, "mymoneyaccount.h.gcov" shows:

    6:   81:class KMM_MYMONEY_EXPORT MyMoneyAccount : public MyMoneyObject, public MyMoneyKeyValueContainer
    ...
    -:  192:  const QString& institutionId(void) const {
    -:  193:    return m_institution;
    -:  194:  }
    ...
    -:  272:  int accountCount(void) const {
#####:  273:    return m_accountList.count();
    -:  274:  };

Maybe I'm wrong, but this result means that gcov is not considering "return m_institution;" as executable code while does it considering "return m_accountList.count()" as executable code. Moreover, it shows "6" in the line of the class declaration, so this file has coverage information but not what I was expecting.

I have to say that maybe the problem has to do with the names of the files and the directories:

1- The object files are created in "CMakefiles/kmm_mymoney.dir" ending with ".cpp.o", for instance, mymoneyaccount.cpp.o. Thus, the "gcno" and "gcda" files are created with the name "mymoneyaccount.cpp.gcno" and "mymoneyaccount.cpp.gcda" (of course, in the directory "CMakefiles/kmm_mymoney.dir").

2- When I execute:
gcov -o CMakeFiles/kmm_mymoney.dir mymoneyaccount.cpp
gcov gives the following error:

mymoneyaccount.gcno:cannot open notes file

So I have to rename those files:
mv mymoneyaccount.cpp.gcno mymoneyaccount.gcno
mv mymoneyaccount.cpp.gcda mymoneyaccount.gcda

Finally, I have another question. When I execute gcov in the file containing the test cases, I mean, "gcov mymoneyaccounttest.cpp" instead of "gcov mymoneyaccount.cpp", I also obtain a "mymoneyaccount.h.gcov" file, but the coverage information is even worse:

#####:   81:class KMM_MYMONEY_EXPORT MyMoneyAccount : public MyMoneyObject, public MyMoneyKeyValueContainer

Anyway, the question is: should I execute "gcov" on the implementation file "mymoneyaccount.cpp" or in the test file "mymoneyaccounttest.cpp"?

Sorry for the length. Thanks.

Best Answer

Thank you very much Tony!! That link had the solution. I just changed the optimization from -O2 to -O0 and now the result is:

1: 192: const QString& institutionId(void) const { 
1: 193:     return m_institution; 
-: 194: } 

I have to note that to obtain "mymoneyaccount.cpp.gcov" I execute "gcov mymoneyaccount.cpp", but to obtain "mymoneyaccount.h.gcov" I have to execute "gcov mymoneyaccounttest.cpp". Do you know why? Both files include "mymoneyaccount.h".

Related Topic