Using libclang as a compiler
I'm working on a tool that generates C code from a speci开发者_高级运维fication. 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?
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 clang
binary itself. It does invoke a libclang library 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 SourceManager
and set with setSourceManager()
method of CompilerInvocation Clang
.
精彩评论