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?
To compile a C++ program on the command line, run the clang++ compiler as follows: $ scl enable llvm-toolset-6.0 'clang++ -o output_file source_file ...' This creates a binary file named output_file in the current working directory. If the -o option is omitted, the clang++ compiler creates a file named a.
LibClang is a stable high level C interface to clang. When in doubt LibClang is probably the interface you want to use. Consider the other interfaces only when you have a good reason not to use LibClang. Canonical examples of when to use LibClang: Xcode.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With