How to dynamically load often re-generated c code quickly?

前端 未结 4 1856
遥遥无期
遥遥无期 2020-12-01 17:00

I want to be able to generate C code dynamically and re-load it quickly into my running C program.

I am on Linux, how could this be done?

Can a library .so f

4条回答
  •  自闭症患者
    2020-12-01 17:55

    Your best bet's probably the TCC compiler, which allows you to do exactly this --- compile source code, add it to your program, run it, all without touching files.

    For a more robust but non-C-based solution, you should probably check out the LLVM project, which does much the same thing but from the perspective of producing JITs. You don't get to go via C, instead using a kind of abstract portable machine code, but the generated code is loads faster and it's under more active development.

    OTOH if you want to do it all manually by shelling out to gcc, compiling a .so and then loading it yourself, dlopen() and dlclose() will do what you want.

提交回复
热议问题