在应用程序中嵌入C ++编译器 [英] Embed C++ compiler in application

查看:137
本文介绍了在应用程序中嵌入C ++编译器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不是着色器凉爽吗?你可以折腾一个纯字符串,只要它是有效的源,它会编译,链接和执行。我想知道是否有一种方法来嵌入GCC内部用户应用程序,以便它是自足,例如。具有内部能力来编译与自身兼容的本地二进制文件。

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.

到目前为止,我一直在调用一个进程的独立GCC,在应用程序中启动,但我想知道如果有一些API或某事可以允许使用直接,而不是一个独立的编译器。

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?

编辑:虽然最初的问题是关于CGG,我会解决如何嵌入LLVM / Clang的信息

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

现在对于无法将2 + 2放在一起的人来说是一个特殊的编辑:这个问题询问如何将GCC或Clang嵌入可执行文件中允许从代码使用内部API,而不是从命令提示符调用编译。

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.

推荐答案

我会为使用Clang / LLVM而不是GCC的建议添加+1。有几个好的理由:

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


  • 它更加模块化和灵活

  • 编译时间可以低于GCC

  • 它支持您在评论中列出的平台

  • 它具有可在内部使用的API

  • it is more modular and flexible
  • compilation time can be substantially lower than GCC
  • it supports the platforms you listed in the comments
  • it has an API that can be used internally
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);

这篇关于在应用程序中嵌入C ++编译器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆