[cfe-dev] Creating DLLs from within an application

John Criswell criswell at illinois.edu
Tue Dec 11 07:42:48 PST 2012


On 12/10/12 10:40 AM, Marco Craveiro wrote:
> Hello cfe-dev,
>
> I'm investigating dynamic code generation and Clang seems to be the
> perfect fit. I couldn't find an answer in Google - but maybe its just
> my Google-fu. Anyway, my scenario is this: I'd like to create a
> program that generates several .c files with trivial C functions.
> These rely on no includes - only arithmetic operations on arrays of
> doubles. I'd like to package up all these functions into a DLL/SO,
> load it and call the functions from within the running application.
> The parameters being passed in will be the arrays to doubles that
> would have already been loaded into memory by the main program.
>
> I just wanted to double-check with the mailing list that all of the
> above can be done by linking at compile time against a clang library
> (static or dynamic); my program would generate the  .c files and the
> clang infrastructure would do the magic to come up with the DLL/SO.
> I'm fairly confident with the rest - traditional DLL/SO loading and
> unloading - unless some one foresees any problems.
>
> Many thanks for your time, and for a great compiler and surrounding
> infrastructure.

Can you generate LLVM IR instead of C code or Clang ASTs?  I'm not 
familiar with the Clang APIs, but I'm pretty sure the LLVM APIs are 
designed to do this (because this is more or less how the LLVM JIT works).

It might also be more efficient as you wouldn't need to go from C source 
-> Clang AST -> LLVM IR -> native code; you could just go from LLVM IR 
-> native code.

Just a thought,

-- John T.


>
> Cheers
>
> Marco




More information about the cfe-dev mailing list