title: how to set include paths with autotools
body: <p>I'm working on a C++ project that uses <code>autoconf</code> &amp; <code>automake</code>, and I'm struggling to correctly set up the include paths in <code>*CPPFLAGS</code>. I've read about 3 hours worth of documents, and I can't figure it out yet. I'm not looking for a hack, but for the correct way to do this. Here is my conundrum.</p>
  
  <p>As I see it, there are 3 completely different sources for include paths:</p>
  
  <ol>
  <li>External libraries that must be installed along with my package, which are configured by <code>configure --with-XXX=&lt;PATH&gt;</code>.</li>
  <li>Within my package, some source files use <code>#include &lt;file.h&gt;</code> even when <code>file.h</code> is part of the package, so to compile them, I must set the include path correctly. (Note, it's not an option to edit all these files.)</li>
  <li>Whimsical (or not) standards specify the user must be allowed to specify their own (extra) include paths. That is, I shouldn't be setting <code>CPPFLAGS</code> at all.</li>
  </ol>
  
  <p>In my current setup:</p>
  
  <ul>
  <li>Type 1 paths are set inside <code>configure.ac</code> by <code>AC_SUBST(CPPFLAGS, "$CPPFLAGS -I&lt;path&gt;")</code>.</li>
  <li>Type 2 paths are set inside <code>Makefile.am</code> by <code>test_CPPFLAGS = -I&lt;path&gt;</code>.</li>
  <li>Type 3 cannot be set. More exactly, if the user sets <code>CPPFLAGS</code> before running <code>make</code>, this overrides Type 1 settings, causing compilation to fail. Of course, the user could try to use <code>CXXFLAGS</code> instead, but that one has a different use (remember, I'm asking for the correct way to do this, not a hack).</li>
  </ul>
  
  <p>I tried to fix this by setting Type 1 paths using <code>AM_CPPFLAGS</code> inside <code>configure.ac</code>. (For reference: if you set <code>AM_CPPFLAGS</code> instead of <code>CPPFLAGS</code>, but you still need to run some checks such as <code>AC_CHECK_HEADERS</code>, you need to temporarily set <code>CPPFLAGS</code> and then revert it for the checks to work; this is explained <a href="http://www.bioinf.uni-freiburg.de/~mmann/HowTo/automake.html#extLibCheck">here</a>.) This frees up <code>CPPFLAGS</code> for Type 3 paths, but unfortunately the compilation fails because the <code>Makefile</code>-s that gets produced by <code>configure</code> will only use <code>AM_CPPFLAGS</code> if no specialized <code>&lt;target&gt;_CPPFLAGS</code> exists. So, if <code>test_CPPFLAGS</code> exists with a Type 2 path, compiling <code>test</code> will fail because it doesn't get the Type 1 path.</p>
  
  <p>A fix would be to specify inside <code>Makefile.am</code> to always use <code>AM_CPPFLAGS</code>. But is this "by the book"? Can I do this in a global way, or do I have to edit every single <code>target_CPPFLAGS</code>? Is there another "correct" solution?</p>