有什么办法来编译在运行时额外code C或C ++? [英] Is there any way to compile additional code at runtime in C or C++?

查看:208
本文介绍了有什么办法来编译在运行时额外code C或C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我想做的事:


  1. 运行程序,并初始化一些数据结构。

  2. 然后编译额外的code可以访问/修改现有的数据结构。

  3. 重复步骤2需要的。

我希望能够既 C C ++ 使用 GCC做到这一点(最终的Java )的类Unix系统(特别是Linux和Mac OS X)。这个想法是基本实现对这些语言的编译前pressions和声明,因为它们是进入并使用它们来修改现有的数据结构(一些是做所有的时间脚本语言)读的eval打印循环。我写在蟒蛇这个工具,它生成 C / C ++ 的文件,但是这不应该是相关的。

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.

我已经探讨与共享库这样做,但据悉,修改共享库不影响已经运行的程序。我一直在使用共享内存也尝试过,但无法找到一个方法来加载功能到堆上。我已经使用汇编code也考虑,但还没有尝试这样做。

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.

我想preFER不使用比 GCC其他任何编译器,除非是绝对没有办法做到这一点在 GCC

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

如果任何人有任何意见或知道如何做到这一点,任何帮助将AP preciated。

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

推荐答案

我想你也许可以这样使用动态库,并在运行时加载它们(使用完成的dlopen 和朋友)。

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);
}

您显然必须编制新的code,你走,但如果你不断替换 mynew code.so 我认为这将为你工作。

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.

这篇关于有什么办法来编译在运行时额外code C或C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆