简体   繁体   English

是否可以在内存中使用Clang C ++ API使用Clang编译源代码?

[英]Is it possible to compile the source with Clang using Clang C++ API in memory?

I'd like to compile the sources using Clang with no executables running, but in-memory via API. 我想使用Clang编译源代码,而不运行任何可执行文件,而是通过API在内存中运行。 I've found post about how to do it but Driver seems to find and run executables like clang / clang++ and ld behind the scene: 我已经找到有关如何执行此操作的帖子 ,但Driver似乎在后台找到并运行了clang / clang++ld类的可执行文件:

The output shows that the Driver is invoking clang to do the compiling and ld to do the linking. 输出显示驱动程序正在调用clang进行编译,而ld进行链接。 As you can see, the Driver adds arguments of its own to each invocation, in addition to the ones we passed in. The -v flag shows you exactly how the compiler and linker are being invoked. 如您所见,驱动程序除了我们传入的参数外,还为每个调用添加了自己的参数。-v标志准确显示了如何调用编译器和链接器。

As i'm going to do it on iOS running new process is prohibited ( fork is prohibited) and paths are completely different. 因为我要在iOS上执行此操作,因此禁止运行新进程(禁止fork ),并且路径完全不同。

So it there any way to compile the sources using Clang API with no executables running? 因此,有没有办法在不运行可执行文件的情况下使用Clang API来编译源代码?

Yes, this is possible. 是的,这是可能的。 It's how most OpenCL implementations work - they use the clang and llvm libraries to build a single executable (typically) that produces code for the hardware that CL runs on. 大多数OpenCL实现都是这样工作的-它们使用clangllvm库来构建单个可执行文件(通常),该可执行文件为运行CL的硬件生成代码。 I believe CUDA does something very similar, but I haven't studied it. 我相信CUDA所做的事情非常相似,但我尚未对其进行研究。

It's not terribly easy, but I guess I could have something up and running in a few days, maybe a couple of weeks worst case. 这并不是一件容易的事,但是我想我可能会在几天内启动并运行某些东西,也许是最坏情况下几周。 I think the biggest problem will be the linking. 我认为最大的问题是链接。 In particular if you don't actually want to produce a executable file, but make an "in memory execution of the code" - which involves allocating memory that has execute rights, and then load the code, which is normally tasks that the linker and loader are responsible for. 特别是如果您实际上不想生成可执行文件,而是进行“代码在内存中执行”,这涉及分配具有执行权限的内存,然后加载代码,这通常是链接器和装载机负责。

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

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