Using libclang as a compiler

I am working on a tool that generates C code from a specification. Therefore, users need to compile the generated code before using the compiled code with another tool. I would like to automate this tedious process. Instead of invoking the process, I wonder if it is possible using libclang to embed the compiler directly?

+6
source share
1 answer

Yes, perhaps for some version of clang / llvm. You can start with http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/driver/cc1_main.cpp?revision=126577&view=markup = this is the source of the clang binary itself. It calls the libclang library, and you can integrate this code into your application. (It actually uses unstable C ++ interfaces from the internal clang and llvm libraries, and not for the stable C libclang API.)

If you save all C sources in a file, that’s all you need. But if you want to transfer sources directly through memory, you must write a custom SourceManager and set the CompilerInvocation Clang using the setSourceManager() method.

+12
source

Source: https://habr.com/ru/post/887266/


All Articles