简体   繁体   English

我可以将 GCC 作为守护程序运行(或将其用作库)吗?

[英]Can I run GCC as a daemon (or use it as a library)?

I would like to use GCC kind of as a JIT compiler, where I just compile short snippets of code every now and then.我想将 GCC 用作 JIT 编译器,我只是不时编译短代码片段。 While I could of course fork a GCC process for each function I want to compile, I find that GCC's startup overhead is too large for that (it seems to be about 50 ms on my computer, which would make it take 50 seconds to compile 1000 functions).虽然我当然可以为我想要编译的每个函数 fork 一个 GCC 进程,但我发现 GCC 的启动开销太大了(在我的电脑上似乎是大约 50 毫秒,这使得编译 1000功能)。 Therefore, I'm wondering if it's possible to run GCC as a daemon or use it as a library or something similar, so that I can just submit a function for compilation without the startup overhead.因此,我想知道是否可以将 GCC 作为守护程序运行或将其用作库或类似的东西,这样我就可以提交一个函数进行编译而无需启动开销。

In case you're wondering, the reason I'm not considering using an actual JIT library is because I haven't found one that supports all the features I want, which include at least good knowledge of the ABI so that it can handle struct arguments (lacking in GNU Lightning), nested functions with closure (lacking in libjit) and having a C-only interface (lacking in LLVM; I also think LLVM lacks nested functions).如果您想知道,我不考虑使用实际 JIT 库的原因是因为我还没有找到支持我想要的所有功能的库,其中至少包括 ABI 的良好知识,以便它可以处理结构参数(在 GNU Lightning 中缺少)、带闭包的嵌套函数(在 libjit 中缺少)和只有 C 语言的接口(在 LLVM 中缺少;我也认为 LLVM 缺少嵌套函数)。

And no, I don't think I can batch functions together for compilation;不,我不认为我可以将函数批处理在一起进行编译; half the point is that I'd like to compile them only once they're actually called for the first time.一半是我只想在第一次真正调用它们时才编译它们。

I've noticed libgccjit , but from what I can tell, it seems very experimental.我已经注意到libgccjit ,但据我所知,它似乎非常具有实验性。

My answer is "No (you can't run GCC as a daemon process, or use it as a library)", assuming you are trying to use the standard GCC compiler code.我的回答是“不(您不能将 GCC 作为守护进程运行,或将其用作库)”,假设您正在尝试使用标准 GCC 编译器代码。 I see at least two problems:我看到至少有两个问题:

  1. The C compiler deals in complete translation units, and once it has finished reading the source, compiles it and exits. C 编译器处理完整的翻译单元,一旦它读完源代码,编译它并退出。 You'd have to rejig the code (the compiler driver program) to stick around after reading each file.在阅读每个文件后,您必须重新调整代码(编译器驱动程序)才能保留下来。 Since it runs multiple sub-processes, I'm not sure that you'll save all that much time with it, anyway.由于它运行多个子进程,因此我不确定您是否会使用它节省那么多时间。

  2. You won't be able to call the functions you create as if they were normal statically compiled and linked functions.您将无法调用您创建的函数,就好像它们是普通的静态编译和链接函数一样。 At the least you will have to load them (using dlopen() and its kin, or writing code to do the mapping yourself) and then call them via the function pointer.至少您必须加载它们(使用dlopen()及其亲属,或编写代码来自己进行映射),然后通过函数指针调用它们。

The first objection deals with the direct question;第一个反对意见涉及直接问题; the second addresses a question raised in the comments.第二个解决了评论中提出的问题。

I'm late to the party, but others may find this useful.我迟到了,但其他人可能会觉得这很有用。

There exists a REPL (read–eval–print loop) for c++ called Cling , which is based on the Clang compiler.存在一个名为Cling 的C++ REPL(读取-评估-打印循环),它基于 Clang 编译器。 A big part of what it does is JIT for c & c++.它所做的很大一部分是用于 c 和 c++ 的 JIT。 As such you may be able to use Cling to get what you want done.因此,您可以使用 Cling 来完成您想做的事情。

The even better news is that Cling is undergoing an attempt to upstream a lot of the Cling infrastructure into Clang and LLVM.更好的消息是,Cling 正在 尝试将许多 Cling 基础设施 上游引入 Clang 和 LLVM。

@acorn pointed out that you'd ruled out LLVM and co. @acorn 指出你已经排除了 LLVM 和 co。 for lack of ac API, but Clang itself does have one which is the only one they guarantee stability for: https://clang.llvm.org/doxygen/group__CINDEX.html由于缺乏 ac API,但 Clang 本身确实有一个,这是他们保证稳定性的唯一一个: https : //clang.llvm.org/doxygen/group__CINDEX.html

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

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