1

Does anyone know how to compile your c++ code wich you write while your program is already running?

And later i would like to run that code.

I want to do this because I am trying to make a game that woul teach you programing and so the user would have to write the code while the game is running and test it.

Thanks for any help

schmru
  • 591
  • 3
  • 8
  • 29
  • You'll need more than one program for that! Alternatively - use web-based compilation (google for *codepad* etc.) – rubber boots Jul 17 '12 at 13:02
  • What's your starting point? Do you have any initial ideas? What have you researched thus far? – Chris Tonkinson Jul 17 '12 at 13:02
  • Well a simple way is to save the text into files, then execute the compiler commands rom the command-shell to compile it, then run the resulting executable the same way – SingerOfTheFall Jul 17 '12 at 13:03

2 Answers2

3

You'd have an easier time if you chose a language that was designed with embedding in mind - like LUA or python. For C++, you'd have to go for something extremely clumsy and fragile like invoking an external compiler (which is also a logistics nightmare when shipping your game), or something as complex as integrating a compiler in your game (probably doable with llvm components, but...)

Also, for "teaching programming", C++ probably isn't the best language :)

snemarch
  • 4,958
  • 26
  • 38
  • I tried to do it with python but i get some error with memry access and i haven't found any solution to that or else i would be using it. – schmru Jul 17 '12 at 14:08
1

You have to call the compiler to compile and link the user-entered code. This has to be made either into an executable that is then run from another process you create, or as a library that you dynamically load and call.

How this is done is different on POSIX platforms (like Linux and OSX) and Windows.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621