title: Is there any way to compile additional code at runtime in C or C++?
body: <p>Here is what I want to do:</p>
  
  <ol>
  <li>Run a program and initialize some data structures.</li>
  <li>Then compile additional code that can access/modify the existing data structures.</li>
  <li>Repeat step 2 as needed.</li>
  </ol>
  
  <p>I want to be able to do this with both <code>C</code> and <code>C++</code> using <code>gcc</code> (and eventually <code>Java</code>) on Unix-like systems (especially Linux and Mac OS X). The idea is to basically implement a read-eval-print loop for these languages that compiles expressions and statements as they are entered and uses them to modify existing data structures (something that is done all the time in scripting languages). I am writing this tool in <code>python</code>, which generates the <code>C</code>/<code>C++</code> files, but this should not be relevant.</p>
  
  <p>I have explored doing this with shared libraries but learned that modifying shared libraries does not affect programs that are already running. I have also tried using shared memory but could not find a way to load a function onto the heap. I have also considered using assembly code but have not yet attempted to do so.</p>
  
  <p>I would prefer not to use any compilers other than <code>gcc</code> unless there is absolutely no way to do it in <code>gcc</code>.</p>
  
  <p>If anyone has any ideas or knows how to do this, any help will be appreciated.</p>