Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using libclang as a compiler

Tags:

llvm

clang

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?

like image 383
Alexandre Hamez Avatar asked May 04 '11 10:05

Alexandre Hamez


People also ask

How do I compile with Clang ++?

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.

What is LibClang?

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.


1 Answers

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.

like image 68
osgx Avatar answered Feb 01 '23 16:02

osgx