0

I realize that this (Is it possible to dynamically compile and execute C# code fragments?) is a similar question, But it is 5 years old, and also in C#. Basically what I would like to do is have a program take code from a text file, and then compile and run it. If C++ is not a good language for this a recommendation would be helpful. Here is my basic goal.

Have code that runs code from a text file(ex; Test0).
Have the code from the file output 'Hello'
Then make another file(Test1) and change the output to a mixing of those letters
When it saves, it overwrites any previous file of the same name
The Code from the text file then ends.
The original program then makes Test0=Test1
The program then loops or ends.

Sorry about the coloring, the only way I know how to make the enter key strokes to appear is the code exert thingy.

Community
  • 1
  • 1
Kuroyuki
  • 21
  • 2
  • Thanks for the edit, Cyber =) – Kuroyuki Mar 04 '15 at 18:31
  • btw quick question, How long does it usually take for a question to be answered? I want to work on my program, but I can't until I know how to run code from a text file. Not complaining. – Kuroyuki Mar 04 '15 at 18:48
  • A programme that takes code from a text file and compiles it is called a compiler. If you want to interactively edit and run it, it's an ide. It's very ambitious and much to broad for here ! – Christophe Mar 04 '15 at 18:58
  • there isn't some way to just have the program treat it as though the code in the text file is part of the program and compile and run it like it always does? – Kuroyuki Mar 04 '15 at 19:06

1 Answers1

2

The most basic solution is to invoke the C/C++ compiler to compile the text file each time it changes and before you call it. But what you are probably looking for is an interpreter, not a compiler. There are various C/C++ interpreters (which you can search for) that may meet your needs. However, C/C++ is not traditionally interpreted. More popular interpreted languages are Python, Lua, and JavaScript. With those, it is possible to embed the source code of the interpreter into your C/C++ program and interact with it --- it is possible to call the interpreted language's functions from C/C++ and vice-versa.

David Ching
  • 1,903
  • 17
  • 21