How to run c++ code from c++?

前端 未结 5 1636
夕颜
夕颜 2021-01-24 02:03

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

5条回答
  •  难免孤独
    2021-01-24 02:14

    Short answer: you can't.

    Slightly longer answer: c++ has no reflection, and is generally compiled, 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.

提交回复
热议问题