I’m working on a tool that generates C code from a specification. Users thus need to compile themselves the generated code before using the compiled code with another tool. I would like to automate this tedious process. Rather than calling a process, I wonder if it is possible, using libclang, to directly embed the compiler?
Share
Yes, it is possible for some version of clang/llvm. You can start from http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/driver/cc1_main.cpp?revision=126577&view=markup = this is a source of
clangbinary itself. It does invoke alibclanglibrary and you can integrate this code into you application. (Actually, it uses unstable C++ interfaces from internal clang and llvm libraries, not the stable C API of libclang.)If you save all C sources in the file, this is all you need. But if you want to pass sources directly via memory, you should write custom
SourceManagerand set withsetSourceManager()method ofCompilerInvocation Clang.