1

I have found a need to execute C++ code in the terminal. When giving thought on how to do this, Python's exec command is perfect. Sadly, I cannot use this as I need a C++ parallel.

Is there a similar command in C++?

  • 2
    No. C++ is generally compiled, not interpreted. – Igor Tandetnik Aug 12 '19 at 01:51
  • Would it be possible to have the code compiled based on a command? Like send `g++ filename` and mandate an `enter` keypress? –  Aug 12 '19 at 01:56
  • 2
    You can certainly have your python program generate C++ code, write it to a file, then invoke `g++` to compile it, and then presuming that everything goes well, execute it. But that sounds like an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What is the real problem you're trying to solve. No, not the one about executing C++ code dynamically, but the problem to which the solution you believe is to execute C++ code dynamically. – Sam Varshavchik Aug 12 '19 at 01:59
  • 1
    Coming from a background of C programming, one would fork the process, and `execvp` within it. Something like https://codereview.stackexchange.com/a/177377 could work for launching an executable. If this is what you're asking for? – avlec Aug 12 '19 at 02:16
  • 1
    [`std::system()`](https://en.cppreference.com/w/cpp/utility/program/system) may come close to what you need, but [isn't always recommended](https://stackoverflow.com/q/19913446/10957435). –  Aug 12 '19 at 02:29
  • Are you asking for an interactive C++ environment where you can type code and see the result immediately without compiling, just for debug or experiment use, like ipython or irb? – Alex Wang Aug 12 '19 at 04:10
  • @Cygnus I don't understand how the accepted answer could possibly answer your question. There is no way to execute code dynamically using C++'s `system` or any of the non-standard functions in the `exec*` family. Those are used to run external commands. Python's `exec` is not. It injects code into a running program. – Ted Lyngmo Aug 12 '19 at 20:38
  • 1
    @TedLyngmo As @Chipster stated in his/her answer - there is no perfect equivalent. The answer given was the closest to `exec` in Python. –  Aug 13 '19 at 19:43
  • @Cygnus It's just that none of those suggestions can be used to do anything remotely close to what `exec` in python does and anyone searching for a Python `exec` equivalent in C++ is going to waste time trying to figure out how to use the answer. At least put some example Python `exec`-using code in the question so it becomes clear in what situations the answer can help for future readers, because I can't think of any. – Ted Lyngmo Aug 13 '19 at 20:40

3 Answers3

3

There is no perfect equivalent of exec() in C++ that I'm aware, as C++ isn't designed to be used this way normally.

system() comes to mind as a near equivalent off the top of my head, although I will warn you that system() is not usually recommended for use in production code.

However, if none of the reasons in the linked question bother you, you could theoretically construct an exec() from a combination of system() and exit():

void exec(const char* command, int code=0) {
    system(command);
    exit(code);
}

Edit: I may be way off. There may be an exec in C++. Also, see this question.

2

The short answer is no, c++ doesn't have support for evaluating and executing arbitrary code.

If you need scripting support user a scripting language. Lua and python are reasonably easy to integrate. chaiscript even looks a lot like c++ code.

You could invoke a compiler from within your program and then run the resulting executable but i doubt that's a good solution to whatever your problem is.

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
1

https://docs.python.org/3/library/functions.html#exec

exec(object[, globals[, locals]])
This function supports dynamic execution of Python code.

There is no equivalent or similar function in C++.

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108