Matt Matt - 22 days ago 4x
C Question

Is there any way to compile additional code at runtime in C or C++?

Here is what I want to do:

  1. Run a program and initialize some data structures.

  2. Then compile additional code that can access/modify the existing data structures.

  3. Repeat step 2 as needed.

I want to be able to do this with both
(and eventually
) 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
, which generates the
files, but this should not be relevant.

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.

I would prefer not to use any compilers other than
unless there is absolutely no way to do it in

If anyone has any ideas or knows how to do this, any help will be appreciated.


I think you may be able to accomplish this using dynamic libraries and loading them at runtime (using dlopen and friends).

void * lib = dlopen("", RTLD_LAZY);
if(lib) {
    void (*fn)(void) = dlsym(lib, "libfunc");

    if(fn) fn();

You would obviously have to be compiling the new code as you go along, but if you keep replacing I think this will work for you.