简体   繁体   English

有没有办法在运行时用C或C ++编译其他代码?

[英]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. 根据需要重复步骤2。

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). 我希望能够在类Unix系统(尤其是Linux和Mac OS X)上使用gcc (最终是Java )同时使用CC++ 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). 我们的想法是基本上为这些语言实现一个read-eval-print循环,它们在输入时编译表达式和语句,并使用它们来修改现有的数据结构(在脚本语言中一直都是这样)。 I am writing this tool in python , which generates the C / C++ files, but this should not be relevant. 我在python编写这个工具,它生成C / C++文件,但这不应该是相关的。

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 . 我宁愿不使用除gcc之外的任何编译器,除非在gcc绝对没有办法。

If anyone has any ideas or knows how to do this, any help will be appreciated. 如果有人有任何想法或知道如何做到这一点,任何帮助将不胜感激。

There is one simple solution: 有一个简单的解决方案:

  1. create own library having special functions 创建具有特殊功能的自有库
  2. load created library 加载创建的库
  3. execute functions from that library, pass structures as function variables 从该库执行函数,将结构作为函数变量传递

To use your structures you have to include same header files like in host application. 要使用您的结构,您必须包含相同的头文件,如在宿主应用程序中。

structs.h: structs.h:

struct S {
    int a,b;
};

main.cpp: main.cpp中:

#include <iostream>
#include <fstream>
#include <dlfcn.h>
#include <stdlib.h>

#include "structs.h"

using namespace std;

int main ( int argc, char **argv ) {

    // create own program
    ofstream f ( "tmp.cpp" );
    f << "#include<stdlib.h>\n#include \"structs.h\"\n extern \"C\" void F(S &s) { s.a += s.a; s.b *= s.b; }\n";
    f.close();

    // create library
    system ( "/usr/bin/gcc -shared tmp.cpp -o libtmp.so" );

    // load library        
    void * fLib = dlopen ( "./libtmp.so", RTLD_LAZY );
    if ( !fLib ) {
        cerr << "Cannot open library: " << dlerror() << '\n';
    }

    if ( fLib ) {
        int ( *fn ) ( S & ) = dlsym ( fLib, "F" );

        if ( fn ) {
            for(int i=0;i<11;i++) {
                S s;
                s.a = i;
                s.b = i;

                // use function
                fn(s);
                cout << s.a << " " << s.b << endl;
            }
        }
        dlclose ( fLib );
    }

    return 0;
}

output: 输出:

0 0
2 1
4 4
6 9
8 16
10 25
12 36
14 49
16 64
18 81
20 100

You can also create mutable program that will be changing itself (source code), recompiling yourself and then replace it's actual execution with execv and save resources with shared memory. 您还可以创建可更改的程序 (源代码),重新编译自己,然后用execv替换它的实际执行,并使用共享内存保存资源。

I think you may be able to accomplish this using dynamic libraries and loading them at runtime (using dlopen and friends). 我认为您可以使用动态库并在运行时加载它们(使用dlopen和朋友)来完成此任务。

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. 您显然必须mynewcode.so编译新代码,但如果您继续更换mynewcode.so我认为这对您mynewcode.so

Even though LLVM is now used today mostly for its optimizations and backend roles in compilation, as its core it is the Low-Level Virtual Machine. 尽管LLVM现在主要用于编译中的优化和后端角色,但它的核心是低级虚拟机。

LLVM can JIT code, even though the return types may be quite opaque, so if you are ready to wrap your own code around it and don't worry too much about the casts that are going to take place, it may help you. LLVM可以JIT代码,即使返回类型可能非常不透明,所以如果你准备好自己包装自己的代码并且不要过多担心将要发生的转换,它可能对你有所帮助。

However C and C++ are not really friendly for this kind of thing. 但是C和C ++对这种事情并不友好。

是的 - 您可以使用Runtime Compiled C ++ (或查看RCC ++博客和视频 )或其中一个替代方案来完成此操作

This can be done portably with OpenCL 这可以通过OpenCL轻松完成

OpenCL is a widely supported standard, mainly used for offloading calculations to specialized hardware, such as GPUs. OpenCL是一种广泛支持的标准,主要用于将计算卸载到专用硬件,如GPU。 However, it also works just fine on CPUs and actually performs run-time compilation of C99-like code as one of its core features (this is how the hardware portability is achieved). 但是,它在CPU上工作得很好,并且实际上执行类似C99的代码的运行时编译作为其核心功能之一(这就是如何实现硬件可移植性)。 The newer versions (2.1+) also accept a large subset of C++14. 较新的版本(2.1+)也接受大量的C ++ 14子集。

A basic example of such run-time compilation & execution might look something like this: 这种运行时编译和执行的基本示例可能如下所示:

#ifdef __APPLE__
#include<OpenCL/opencl.h>
#else
#include<CL/cl.h>
#endif
#include<stdlib.h>
int main(int argc,char**argv){//assumes source code strings are in argv
    cl_int e = 0;//error status indicator
    cl_platform_id platform = 0;
    cl_device_id device = 0;
    e=clGetPlatformIDs(1,&platform,0);                                      if(e)exit(e);
    e=clGetDeviceIDs(platform,CL_DEVICE_TYPE_ALL,1,&device,0);              if(e)exit(e);
    cl_context context = clCreateContext(0,1,&device,0,0,&e);               if(e)exit(e);
    cl_command_queue queue = clCreateCommandQueue(context,device,0,&e);     if(e)exit(e);
    //the lines below could be done in a loop, assuming you release each program & kernel
    cl_program program = clCreateProgramWithSource(context,argc,(const char**)argv,0,&e);
    cl_kernel kernel = 0;                                                   if(e)exit(e);
    e=clBuildProgram(program,1,&device,0,0,0);                              if(e)exit(e);
    e=clCreateKernelsInProgram(program,1,&kernel,0);                        if(e)exit(e);
    e=clSetKernelArg(kernel,0,sizeof(int),&argc);                           if(e)exit(e);
    e=clEnqueueTask(queue,kernel,0,0,0);                                    if(e)exit(e);
    //realistically, you'd also need some buffer operations around here to do useful work
}

If nothing else works - in particular, if un-loading a shared library ends up not being supported on your runtime platform, you could do it the hard way. 如果没有其他工作 - 特别是,如果卸载共享库最终在运行时平台上不受支持,那么您可以通过艰难的方式完成。

1) use system() or whatever to execute gcc or make or whatever to build the code 1)使用system()或其他任何方法来执行gcc或make或其他任何来构建代码

2) either link it as a flat binary or parse whatever format (elf?) the linker outputs on your platform yourself 2)要么将其链接为平面二进制文件,要么解析链接器自己在平台上输出的任何格式(elf?)

3) get yourself some executable pages, either by mmap()'ing an executable file or do doing an anonymous mmap with the execute bit set and copying/unpacking your code there (not all platforms care about that bit, but let's assume you have one that does) 3)通过mmap()执行可执行文件获取一些可执行页面,或者使用执行位设置执行匿名mmap并在那里复制/解压缩代码(并非所有平台都关心这一点,但让我们假设你有一个做)

4) flush any data and instruction caches (since consistency between the two is typically not guaranteed) 4)刷新任何数据和指令缓存(因为通常不保证两者之间的一致性)

5) call it via a function pointer or whatever 5)通过函数指针或其他任何方式调用它

Of course there's another option too - depending on the level of interaction you need, you could build a separate program and either launch it and wait for the result, or fork off and launch it and talk to it by pipes or sockets. 当然还有另一种选择 - 根据您需要的交互级别,您可以构建一个单独的程序,然后启动它并等待结果,或者分叉并启动它并通过管道或套接字与它通信。 If this would meet your needs, it would be a lot less tricky. 如果这可以满足您的需求,那就不那么棘手了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM