As part of development for an application of mine that generates C source code, I need to be able to embed a compiler into my application so that it can generate executable object code. Since this is a sandboxed app intended for the Mac App Store, I cannot use NSTask
to directly invoke clang
, as the standalone tool apparently does not work under a sandbox. Pity.
I came across libclang
, a library version of the compiler, which seems to do the trick. Using information from here, I was able to grab a preconfigured libclang
binary to integrate into my application. Trouble is, I can't seem to figure out what the libclang
API calls are that process and generate an executable. I found this sample code:
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);
Unfortunately, though, it won't compile. I get these errors:
Maybe I'm not including the right header files, I don't know. But given the lack off documentation for libclang
, I would very much appreciate some help.