tags:

views:

200

answers:

5

Hi There, I came here to ask this question because this site has been very useful to me in the past, seems to have very knowledgeable users who are willing to discuss a question even if it is metaphysical at times. And also because googling it did not work.

Java has a compiler and then it has a JDT library that can compile java on the fly (for example used in JasperReports to turn a report template into Java code).

My question: Does anyone know of a library/project that would offer compiling as a set of library classes in c/c++. For example: a suite of classes to perform Preprocessing, Parsing, CodeOptimization and of course Binary rendering to executable images such as ELF or Win format. Basically something that would allow one to compile c or c++ scriptlets as part of an application.

+5  A: 

You might be able to adapt something from the LLVM project to your needs.

Jack Kelly
+12  A: 

Yes: llvm. In particular, clang. At least, that's how they advertise the projects. Also, check this question. It might be relevant if you decide to use llvm.

andref
+5  A: 

You can just require that a compiler be installed, then call it. This is a fairly hefty requirement, but about the only way to truly "embed" C or C++. There are interpreters that you may be able to embed, but that currently seems a poor choice, not the least because any libraries used in the script must have development versions (i.e. headers and source/compiled-libraries) installed, and those libraries could be restricted to the feature set supported by the interpreter (e.g. quality of template implementation).

You're better off using a language like Python or Lua to embed.

Roger Pate
Why the downvote? (I really want to know if there's a problem with my answer.)
Roger Pate
+1 for mentioning Python as a suitable embedded language. Using Python in this case is probably cheaper on resources than compiling on the fly.
Rafe Kettler
Maybe your answer was downvoted because invoking a separate compiler is not "`the only` way to truly embed C or C++".
andref
@andref: But it is. Ch doesn't support all of the C++ language and clang *is* a compiler.
Roger Pate
Clearly, clang (the driver) is proof that you can use the libraries provided clang (the project) and parse, lex, optimize and codegen C++. It might not be as easy as invoking an external compiler, but 640KB asked explicitly for a library. See http://clang.llvm.org/features.html#applications.
andref
@andref: I still see no problem with "...about the only way" to get all of the features of C and C++, including standard libraries and third party libraries — the importance of which the OP hasn't mentioned one way or other, but which I consider a requirement if left unstated. I notice you left out "about" when you quoted me and I should have emphasized "truly".
Roger Pate
I noticed the "about", I was just guessing the reason for the vote down.
andref
+2  A: 

There is the ch interpreter but I have not used it. Generally for scripting type applications a more natural scripted language is used.

rerun
A: 

Great. It looks like LLVM is what I was after. Thanks a lot for your feedback. I am not primarily after C++ as a scripting language. I have noticed that Python is used as an embedded script engine. My primary reason is two fold:

  1. Get rid off Make,CMake and the hell that is Autoconf and replace it with something like Scons that binds into and interacts with all phases of compiling
  2. Hook into the compiling process after parsing and auto generate code. Specificaly meta related code. In my case I have been able to implement almost every feature of Java in C++ except one: Reflection.

Why impose on your code uneeded bload like RTTI that is often inadequate. Instead one could selectively generate added features. But developer would have to choice when and how to use this extra code.

640k
Oh, dear gods. You are fighting C++. Now I feel bad for my answer: it's gonna bring me rivers of bad karma.
andref
Hehe, don't worry about your karma. It is more the other way around.
640KB