title: How to get complex C pre-processor behavior to simplify dynamic program assembly
body: <p>I am writing a program to assemble code for another dynamic program written in the OpenCL language.  For the purpose of this question, let's just assume the dynamic program language is C99 with the restriction that all code must be brought into a single compilation block.</p>
  
  <p>My larger program determines a sequence of steps that will occur in the main function and which C99 source files are needed to implement those steps.  It then invokes a C99 preprocessor / compiler to produce the dynamic program executable.  I can supply the compiler macro definitions using the <code>-D SYMBOL=defn</code> calling argument.</p>
  
  <p>Here is an example scheme that works for me and avoids diving into utilizing a separate pre-processing library or other hefty string manipulation tools.  I would dynamically create the string listed as mainfile.c, the other included files would actually exist.</p>
  
  <p><strong>Is there any way I can combine the separate declaration and definition files into one file?</strong></p>
  
  <pre class="lang-cpp prettyprint-override"><code>// mainfile.c
  #include "ParentDeclarationFile.txt"
  #include "ChildDeclarationFile.txt"
  #include "ParentDefinitionFile.txt"
  #include "ChildDefinitionFile.txt"
  
  int main()
  {
      return aFunctionParent( 5 );
  }
  
  // ParentDeclarationFile.txt
  typedef float Type1;
  int aFunctionParent( Type1 t );
  
  // ChildDeclarationFile.txt
  int aFunctionChild( Type1 t );
  
  
  // ParentDefinitionFile.txt
  int aFunctionParent( Type1 t ) { return aFunctionChild( t ); }
  
  // ChildDefinitionFile.txt
  int aFunctionChild( Type1 t ) { return 2*t; }
  </code></pre>
  
  <p>I'd like to have a single file like this that I can #include twice.</p>
  
  <pre class="lang-cpp prettyprint-override"><code>// SingleParentFile.txt
  #ifdef DECLARATION_MODE
      //declarations here
  #else
      // definitions here
  #endif
  </code></pre>
  
  <p>but <em><code>#define</code> definitions do not pass into #include'd files</em> ( <strong>edit: tured out to be incorrect.</strong> )and supplied <code>-D</code> definitions can't change in the middle of pre-processing.  Right?
  Is there any sort of preprocessor state variable that I could permute after the first pass?</p>
  
  <p>Note: I tried to provide a simplified example that requires the declaration and definition sections appear in separate code locations. The intent of my question is not to find a way around that in this simple example. </p>