tags:

views:

218

answers:

5

If I have some c++ code as a string quantity (data) in a c++ program, can I execute the contents of that string?

As using the CodeDOM in C# or the eval function present in perl, python, etc..

+3  A: 

if you mean compile C++ code on the fly and run it? sure if you have a compiler available to you

Keith Nicholas
i mean that u have a string and you can compile it, like CodeDom in C#
Chad
This is the closest I can imagine doing, compiling the code and dynamically linking the new DLL. It's hardly very safe though.
Twisol
C++ has no in-built reflective capabilities like C#, so it's going to be very difficult no matter what.
Twisol
I don't see what "reflective capabilities" have to do with it at all. Ideally, you'd take C++ code, compile it in-memory, and get a pointer to its `main` (or any other exported symbol of your choice). Tiny C Compiler does precisely that for C. It's certainly doable for C++, just complicated, and no-one bothered so far. I guess if you really try hard, you could extract everything necessary out of g++ (except for the command line binary), and use that.
Pavel Minaev
@Pavel - the reflection is relevant in terms of accessing variables from the main app. Of course, having to explicitly specify what is accessible and how is a *very* good idea, but you have to admit (and already did) - it's not exactly trivial to do as with some scripting language please-exploit-this-gaping-security-hole solutions.
Steve314
+4  A: 

No. As C++ is a static language, you can not dynamically eval arbitrary code.

You could interpret it or even compile and run it seperately as Keith suggested

Charlie Somerville
This answer just doesn't make any sense. "You cannot dynamically eval" but "you could interpret" - what's that supposed to mean? Oh, and of course you can write an evaluator for C++ expressions, or even a full-fledged C++ interpreter... it's just there's no full-featured premade one out there.
Pavel Minaev
+2  A: 

Non of the mainstream C++ implementations have that feature, as C++ is not reflective.

However, take a look at Ch, it might be what you are looking for:

http://www.softintegration.com/

http://en.wikipedia.org/wiki/Ch_interpreter

You can embed Ch interpreter into your C++ application and run dynamic C++ code inside it.

Milan Babuškov
If you don't have to embed C++, I would recommend that you consider embedding lua or python - both are very easy to integrate with C++.
garethm
+9  A: 

Short answer: you can't.

Slightly longer answer: c++ has no reflection, and is a compiled language, so there so is no support for this kind of thing, and it can not be easily added..

Work arounds:

  1. Use an embeddable dynamic language like [python|tcl|ruby|...] in concert with your c++ code. Now you need to have the dynamic language (rather then c++) in the data.
  2. Use a c++ interpreter like cint or ch. This binds you to the interpreter.
  3. Use the system c++ compiler to construct a dynamic library from your code and link to it on the fly. Risky and system dependent.
  4. Use a different language.
dmckee
+2  A: 

C++ cannot do this - the compiled program has no knowledge of the source language syntax, identifier names etc.

Even if you go to the effort of writing the string out to a file, calling a c++ compiler to generate a dynamic library and loading and calling it, that call won't have any knowledge of your program - it won't be able to reference your variables etc.

The most common reason to want to do this is to be able to evaluate expressions from within strings. Writing the code to do this from scratch is definitely non-trivial, but depending on your specific requirements, you should be able to find a library or embeddable scripting language to do roughly what you need.

After a quick Google, I found this - C rather than C++, and I don't know how good it is. It's written as a demo of a parser generator that I haven't heard of. You might find alternatives as demos of better known parser generators such as yacc, bison, yacc++ or antlr.

Steve314