title: How to use gcov with gcc without the optimization?
body: <p>I have a test code like this:</p>
  
  <pre><code>int diff21(int n)
  {
      if (n &lt;= 21)
      {
          return 21 - n;
      }
      else
      {
          return (n - 21) * 2;
      }
  }
  </code></pre>
  
  <p>and I added the compile flags: <code>-fprofile-arcs -ftest-coverage</code> to provide coverage data.</p>
  
  <p>I wrote the test code like this:</p>
  
  <pre><code>CU_ASSERT(diff21(19) == 2);
  </code></pre>
  
  <p>and I used lcov to generate a html report, and it said the <code>else</code> branch's <code>return (n - 21) * 2</code> is excuted, so I got a 100% line coverage. <strong>This is not true</strong></p>
  
  <p>I googled and found that is because of <a href="http://gcc.gnu.org/onlinedocs/gcc-4.5.4/gcc/Gcov-and-Optimization.html#Gcov-and-Optimization" rel="nofollow">this</a> --- the gcc optimization can eliminate some simple code lines by combining them with other lines, <strong>but how should I generate the coverage data if I'm not supposed to use the <code>-fprofile-arcs -ftest-coverage</code> flag?</strong></p>
  
  <p>btw I'm using netbeans and cygwin to do unit tests, I don't think manually calling <code>gcov</code> in a makefile or script is good for me.</p>
  
  <p>just for your information, I'm using lcov like this:</p>
  
  <pre><code>#!/bin/bash
  
  rm -rf coverage_report
  rm -f app.info
  
  lcov -b . -d . -c -o app.info
  mkdir coverage_report
  genhtml --legend --highlight -o coverage_report app.info
  rm -f app.info
  lcov -d . -z
  </code></pre>