Question

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 .

Was it helpful?

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.)

OTHER TIPS

The short answer is "no, you can't do that". C and C++ were never designed to do this.

That's pretty much also the long answer to the actual question, but I'll expand a bit on a few ideas.

The code, as compiled by the compiler is pretty certainly not trivial to add things to. There are a few techniques that can be used to "add more code" to a program:

  1. Add a dynamic shared library (DLL), which contains code that has been compiled separately to the existing code. You could of course also have code in your program to output some code, compile this code with the compiler, link it into a dynamic library, and load it in your code.

  2. You could build your own little code-generator that generates machine code in a chunk of memory. Note that you probably need to call a "special" memory allocation function, as "normal" memory allocations are typically not allowed to be executed - you need to allocate "with execute permission" - VirtualAlloc in Windows does have such a flag, and mmap in Linux/Unix flavours does too. And of course, you pretty much have to "be a compiler" to achieve this.

  3. You could naturally also invent your own interpreted language, which would allow your program to load in for example a text-file with commands/instructions to be executed, or contain text inside the program for execution with this language.

But like I said to start with, this is not what C and C++ (and most other compiled languages) were meant for, so it's not going to be as simple as "stick some C++ code in a string, and make it run".

It depends why you want to do this.

If it's for efficiency reasons - you know what a function does only at run time, but it has to be very efficient - then what was already suggested (writing to a file, compiling to a dll / so and dynamically loading it) is your best option.

BUT if the reason you want this is to allow for user-input behaviour, say a general function your read from a database (behaviour or a unit ingame? value of a field in a plot?) - or more generally you just want to change / augment behaviour at runtime with little concern for efficiency, I recommend using an outside scripting language like lua, which easily interacts with your compiled C++ code.

The C and C++ languages compile to binary machine code, unlike Java and C# which generate instructions for a 'virtual machine' or interpreted scripting languages such as JavaScript. The compilation of C++ is performed by a separate executable, the compiler, which is not incorporated into the resulting executable.

So the language does not have any built in "eval" capability to translate further code once compilation is finished.

It's not uncommon for new C/C++ programmers to think they need to do this, but they typically don't. Perhaps you could expand further on what you're actually looking to do.

But if you do actually need to be able to do this, your options are:

  1. Write code to compile a new executable with the new code and then run the resulting program.
  2. Write a simple parser and "virtual machine" of your own,
  3. Look at incorporating an embedded scripting/interpreted language such as Lua,
  4. Try and wrap your head around integrating CINT,

See also: Scripting language for C++

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top