title: Compile-time test if function is optimized out
body: <p>I'm writing a small operating system for microcontrollers in C (not C++, so I can't use templates). It makes heavy use of some gcc features, one of the most important being the removal of unused code. The OS doesn't load anything at runtime; the user's program and the OS source are compiled together to form a single binary.</p>
  
  <p>This design allows gcc to include only the OS functions that the program actually uses. So if the program never uses i2c or USB, support for those won't be included in the binary.</p>
  
  <p>The problem is when I want to include optional support for those features without introducing a dependency. For example, a debug console should provide functions to debug i2c if it's being used, but including the debug console shouldn't also pull in i2c if the program isn't using it.</p>
  
  <p>The methods that come to mind to achieve this aren't ideal:</p>
  
  <ul>
  <li><p>Have the user explicitly enable the modules they need (using <code>#define</code>), and use <code>#if</code> to only include support for them in the debug console if enabled. I don't like this method, because currently the user doesn't have to do this, and I'd prefer to keep it that way.</p></li>
  <li><p>Have the modules register function pointers with the debug module at startup. This isn't ideal, because it adds some runtime overhead and means the debug code is split up over several files.</p></li>
  <li><p>Do the same as above, but using weak symbols instead of pointers. But I'm still not sure how to actually accomplish this.</p></li>
  <li><p>Do a compile-time test in the debug code, like:</p>
  
  <pre><code>if(i2cInit is used) {
      debugShowi2cStatus();
  }
  </code></pre></li>
  </ul>
  
  <p>The last method seems ideal, but is it possible?</p>