title: Use GNU m4 at compile time for large C/C++ project
body: <h1>Background</h1>
  <p>I have a large project that I'm currently refactoring. One of the problems is massive abuse of string literals (300+ instances) for common paths used by the product (i.e. references to <code>/opt/dev</code> and sub-paths of this location) all over the place, including:</p>
  <ul>
  <li>C/C++ source and header files.</li>
  <li>python scripts.</li>
  <li>bash scripts.</li>
  <li>systemd scripts.</li>
  <li>Binary images (drivers).</li>
  </ul>
  <hr />
  <h1>Goal</h1>
  <p>I'd like to use GNU <a href="/questions/tagged/m4" class="post-tag" title="show questions tagged &#39;m4&#39;" rel="tag">m4</a> to define a file with a set of common macros so we can &quot;write once, use everywhere&quot;, so that if our code is branched (typical for a new product), we can just change the macros in that one file, instead of hunting down the duplicate string literals and debugging them for days. This is easy enough for simple things like scripts, as I just create a copy of the source tree in the build output path, and run M4 directly on the scripts.</p>
  <hr />
  <h1>Problem</h1>
  <p>The only downside so far is that it looks like this will be a massive effort for C/C++ projects, because I only want to execute the <code>M4</code> macro expansion/substitution at compile time. This means that, in order to avoid modifying the source files checked out from git, I'll have to modify my build scripts (lots of Makefiles) to work on a temporary copy of the code rather than the original source files themselves. Otherwise, running <code>M4</code> directly on the version-controlled source will generate a change to the local working copy, and developers are likely to commit this M4-evaluated copy of the code to version control, breaking the M4 effort.</p>
  <hr />
  <h1>Question</h1>
  <p>Is it possible to instruct <a href="/questions/tagged/gcc" class="post-tag" title="show questions tagged &#39;gcc&#39;" rel="tag">gcc</a> to evaluate <a href="/questions/tagged/m4" class="post-tag" title="show questions tagged &#39;m4&#39;" rel="tag">m4</a> macros/expansions during compilation, either via some external tool or some feature of GCC itself? If not, my build scripts will likely require a massive overhaul to support M4 macros.</p>