c++gccembedexecutable

Embed C++ compiler in application


Aren't shaders cool? You can toss in just a plain string and as long as it is valid source, it will compile, link and execute. I was wondering if there is a way to embed GCC inside a user application so that it is "self sufficient" e.g. has the internal capability to compile native binaries compatible to itself.

So far I've been invoking stand alone GCC from a process, started inside the application, but I was wondering if there is some API or something that could allow to use "directly" rather than a standalone compiler. Also, in the case it is possible, is it permitted?

EDIT: Although the original question was about CGG, I'd settle for information how to embed LLVM/Clang too.

And now a special edit for people who cannot put 2 + 2 together: The question asks about how to embed GCC or Clang inside of an executable in a way that allows an internal API to be used from code rather than invoking compilation from a command prompt.


Solution

  • I'd add +1 to the suggestion to use Clang/LLVM instead of GCC. A few good reasons why:


    string source = "app.c";
    string target= "app"; 
    
    llvm::sys::Path clangPath = llvm::sys::Program::FindProgramByName("clang");
    
    // arguments
    vector<const char *> args;
    args.push_back(clangPath.c_str());
    args.push_back(source.c_str());
    args.push_back("-l");
    args.push_back("curl");
    
    clang::TextDiagnosticPrinter *DiagClient = new clang::TextDiagnosticPrinter(llvm::errs(), clang::DiagnosticOptions());
    clang::IntrusiveRefCntPtr<clang::DiagnosticIDs> DiagID(new clang::DiagnosticIDs());
    clang::DiagnosticsEngine Diags(DiagID, DiagClient);
    
    clang::driver::Driver TheDriver(args[0], llvm::sys::getDefaultTargetTriple(), target, true, Diags);
    
    clang::OwningPtr<clang::driver::Compilation> c(TheDriver.BuildCompilation(args));
    
    int res = 0;
    const clang::driver::Command *FailingCommand = 0;
    if (c) res = TheDriver.ExecuteCompilation(*c, FailingCommand);
    if (res < 0) TheDriver.generateCompilationDiagnostics(*c, FailingCommand);