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

后端 未结 6 1990
执笔经年
执笔经年 2020-12-24 02:45

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 st
6条回答
  •  感情败类
    2020-12-24 03:35

    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.

提交回复
热议问题