title: Dynamic #include based on macro definition
body: <p>I'm writing a C++ application in which I want to let the developer choose what algorithm to use for a particular problem at compile time. Both algorithms are implemented as C++ classes that implement a common interface, and are drop-in replacements for each other. They both have a .h and a .cpp file, and reside in a subdirectory (let's call it <code>impl/</code>).</p>
  
  <p>In my Makefile, I have something along the lines of this:</p>
  
  <pre><code>...
  IMPL = default
  ...
  binary: ... impl/$(IMPL).o
  ...
  impl/%.o: impl/%.cpp impl-interface.h impl/%.h
  ...
  %o: %.cpp ...
      $(CXX) $(CXXFLAGS) -DIMPL=$(IMPL) -c -o $@ $*.cpp
  </code></pre>
  
  <p>The idea is that the user should be able to type <code>make binary IMPL=thatimpl</code>.</p>
  
  <p>In whatever files wants to use the algorithm the user has chosen, I then do:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>IImpl o = new IMPL();
  </code></pre>
  
  <p>However, this requires me to include the header file for the chosen implementation. Unfortunately, C++ requires <code>#include</code> to be followed by either a <code>"string"</code>, a <code>&lt;libfile&gt;</code>. You can also use a macro as suggested <a href="https://stackoverflow.com/questions/4743822/generate-include-file-name-in-a-macros">here</a>, but it requires the argument to the macro to be a literal string. If I use:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>#define QUOTEME(M)       #M
  #define INCLUDE_FILE(M)  QUOTEME(impl/##M##.h)
  #include INCLUDE_FILE(IMPL)
  </code></pre>
  
  <p>The compiler will try to include the literal string <code>impl/IMPL.h</code>, rather than expanding <code>IMPL</code> to whatever was passed to <code>make</code> and then to the compiler.</p>
  
  <p>Any pointers on how I might achieve this would be very welcome!</p>