您可以动态编译C代码并将其链接/加载到C程序中吗? [英] Can you dynamically compile and link/load C code into a C program?

查看:57
本文介绍了您可以动态编译C代码并将其链接/加载到C程序中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读到有关 C语言中的评估,这很有意义,如果您编写C字符串解析器/评估器,您可以将其映射到主C程序中的特定函数.但实际上并没有按照我的理解将其放入可执行内存中,例如 JIT编译器似乎可以.我对JIT编译器不完全了解(我从来没有做过),但我了解到要点.

I read about eval in C, and it makes sense that if you write a C string parser/evaluator, you can just map it to specific functions in your main C program. But it doesn't actually place it into executable memory from what I understand, like a JIT compiler seems to do. I don't fully understand JIT compilers (I never made one) but I get the gist.

所以我想知道的是,您是否可以在C中创建某种JIT编译器,而无需进行过多的解析C字符串并将其转换为AST和其他事情的工作.基本上,您可以像在JavaScript中那样做并动态创建一个函数(在C中),以使该函数与任何其他C函数完全相同(即,将其直接编译到驱动器中的可执行计算机代码中).程序的可执行部分).

So what I'm wondering is if you can create sort of a JIT compiler in C without doing too much work of parsing C strings and converting to ASTs and whatnot. Basically, can you do like in JavaScript and dynamically create a function (in C), such that that function is exactly the same as any other C function (i.e. it is compiled directly into executable machine code in the executable part of the program sort of thing).

如果不可能这样做,第二种方法是动态加载C导入/文件/模块.因此,您将启动一个告诉clang编译器编译某些库函数的过程,然后在完成后不停止当前程序的情况下,它将新程序库链接/附加到其自身,从而可以执行代码那样.

If it's not possible to do that, a second approach would be to dynamically load C imports/files/modules. So you spin off a process that tells the clang compiler to compile some library function(s), and after it's done, without stopping the current program, it links/attaches that new program library to itself, and so can execute the code that way.

如果不可能,则可能的选择是简单地在后台重新编译程序,然后将当前程序与从头启动的新程序交换.不过那将是非常原始的.

If that's not possible, maybe an option is to simply recompile the program in the background, then swap the current program with the new program which boots up from scratch. That would just be very primitive though.

试图弄清楚在C中您自己的自定义函数数据类型是否具有某些结构,然后如何以最优化的方式在C中执行该函数.

Trying to figure out if you have some structs for your own custom function datatype in C, how you can then execute that function in C in the most optimized way.

推荐答案

在POSIX系统(Linix,Mac,UNIX)上,您可以使用dlopendlsym函数.这些函数可用于在运行时加载共享库并从中执行一个函数.

On POSIX systems (Linix, Mac, UNIX) you have the dlopen and dlsym functions you can work with. These functions can be used to load a shared library at run time and execute a function from it.

就创建一个库而言,最简单的方法是将相关的源代码写入文件,在单独的进程中运行gcc进行编译,然后使用dlopen/dlsym来运行功能它包含.

As far as creating a library, the simplest thing to do would be to write the relevant source code to a file, run gcc in a separate process to compile it, then use dlopen/dlsym to run the functions it contains.

例如:

#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>

const char *libsrc =
"#include <stdio.h>\n"
"\n"
"void f1()\n"
"{\n"
"  printf(\"in f1\\n\");\n"
"}\n"
"\n"
"int add(int a, int b)\n"
"{\n"
"  return a+b;\n"
"}\n";

int main()
{
    FILE *libfile = fopen("mylib.c", "w");
    fputs(libsrc, libfile);
    fclose(libfile);
    system("gcc -fPIC -shared -g -Wall -Wextra -o libmylib.so mylib.c");

    void *lib = dlopen("libmylib.so", RTLD_NOW);
    if (!lib) {
        printf("dlopen failed: %s\n", dlerror());
        return 1;
    }

    void (*f)() = dlsym(lib, "f1");
    if (f) {
        f();
    } else {
        printf("dlsym for f1 failed: %s\n", dlerror());
    }

    int (*a)(int, int) = dlsym(lib, "add");
    if (a) {
        int x = a(2,3);
        printf("x=%d\n", x);
    } else {
        printf("dlsym for add failed: %s\n", dlerror());
    }

    dlclose(lib);
    return 0;
}

这篇关于您可以动态编译C代码并将其链接/加载到C程序中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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