title: C++ program to dump a boiler plate C++
body: <p>I have a C++ program which dumps out a C++ program. Some of the functions are boiler plate code and certain functions has boiler plate code and code tailored based on a few variables.</p>
  
  <p>A simplified example is presented below:</p>
  
  <pre><code>// Snippet: 1
  #include &lt;fstream&gt;
  
  using namespace std;
  int main()
  {
      ofstream fout("output.cc");
  
      // bp() has just boiler plate code
      fout &lt;&lt; "void bp() {" &lt;&lt; endl;
      fout &lt;&lt; "std::cout &lt;&lt; \"Hello World!\" &lt;&lt; std::endl" &lt;&lt; endl;
      // a few hundred lines of C++ code send to fout
      fout &lt;&lt; "}" &lt;&lt; endl;
  
      // mix() has boiler plate + some custom code
      int size = 4096;
      fout &lt;&lt; "void mix() {" &lt;&lt; endl;
      fout &lt;&lt; "char buffer[" &lt;&lt; size &lt;&lt; "];" &lt;&lt; endl;
      // a few hundred lines of C++ code send to fout
      fout &lt;&lt; "}" &lt;&lt; endl;
  
      // compile output.cc into *.so and delete output.cc
  
      return 0;
  }
  </code></pre>
  
  <p>The <code>output.cc</code> gets compiled and user gets the <code>*.so</code> file. The user does not have access to <code>output.cc</code>. </p>
  
  <p>I wanted to rewrite this since it is difficult to read the boiler plate code when it is inside <code>fout</code> and having escaped quotes makes it a nightmare. Hence I thought of storing the functions in a separate file. For example have <code>bp()</code> in <code>bp.cc</code>:</p>
  
  <pre><code>// file: bp.cc
  void bp() {
      std::cout &lt;&lt; "Hello World" &lt;&lt; std::endl
      // a few hundred lines of C++ code 
  }
  </code></pre>
  
  <p>Then the main file can be <a href="https://stackoverflow.com/questions/10195343/copy-a-file-in-an-sane-safe-and-efficient-way">written</a> as </p>
  
  <pre><code>int main()
  {
      std::ifstream  src("bp.cc");
      std::ofstream  dst("output.cc");
  
      dst &lt;&lt; src.rdbuf();
  }
  </code></pre>
  
  <p>In case of <code>mix()</code> I would use the <a href="http://bit.ly/1bJJ21L" rel="nofollow">Form-Letter Programming</a> by storing the function <code>mix()</code> in <code>mix.cc</code>.</p>
  
  <p>When the functions <code>bp()</code> and <code>mix()</code> were dumped using <code>fout</code> as in <code>Snippet:1</code>, all I had to do was ship the executable since the <code>Snippet:1</code> is self-contained. But</p>
  
  <ul>
  
  <li>If I split the functions into separate files `bp()` into `bp.cc` and `mix()` into `mix.cc` how do I ship it as a single executable? I need to ship `bp.cc` and `mix.cc` along with the executable. I do not want the user to access `bp.cc` and `mix.cc`.</li>
  
  <li> Is there a better way to rewrite the `Snippet:1` than what I have suggested to better suit my needs?
  </ul>