c++quine

C++ add new code during runtime


I am using C++ (in xcode and code::blocks), I don't know much. I want to make something compilable during runtime.

for eg:

char prog []={"cout<<"helloworld " ;} 

It should compile the contents of prog. I read a bit about quines , but it didn't help me .


Solution

  • It's sort of possible, but not portably, and not simply. Basically, you have to write the code out to a file, then compile it to a dll (invoking the compiler with system), and then load the dll. The first is simple, the last isn't too difficult (but will require implementation specific code), but the middle step can be challenging: obviously, it only works if the compiler is installed on the system, but you have to find where it is installed, verify that it is the same version (or at least a version which generates binary compatible code), invoke it with the same options that were used when your code was compiled, and process any errors.

    C++ wasn't designed for this. (Compiled languages generally aren't.)