Matt Matt - 2 months ago 6
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
C
and
C++
using
gcc
(and eventually
Java
) 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
python
, which generates the
C
/
C++
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
gcc
unless there is absolutely no way to do it in
gcc
.

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

Answer

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

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

    if(fn) fn();
    dlclose(lib);
}

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

Comments