1

I want to make a "dynamic" C++ program. My idea is to compile it and after compilation (maybe with scripting language like python) to change some how the code of the program. I know you will tell me that after the compilation I can not change the code but is there a way of doing that?

4
  • idk youtube.com/watch?v=f9Xfh8pv3Fs&feature=g-u-u
    – SRN
    Commented Aug 24, 2012 at 8:35
  • Do you mean something like Cling?
    – Mankarse
    Commented Aug 24, 2012 at 8:48
  • You can change the code at run-time if the code-memory is writable, but it is extremely difficult and very error prone to use it for something useful. It is a way to make exploids (if the OS allows it).
    – stefaanv
    Commented Aug 24, 2012 at 8:49
  • 1
    It is not uncommon to write C++-code that does the "heavy-lifting" (e.g. numeric computation, pathfinding, data compression, ...) and call these functions from scripting languages (take a look at boost.org/doc/libs/1_51_0/libs/python/doc/index.html). I have never heard of any domain where you would want to change the C++-code with a scripting language. Maybe you just want to separate your project into a (large) lib and a (small) executable. Commented Aug 24, 2012 at 8:50

3 Answers 3

4

You could design a modular architecture using plugins in external libraries. For example you could use the Command Pattern. That way you can dynamically load code that was generated after you main program. You would have to fix an interface though. Functions like GetProcAddress in the Windows api might be a good point to start.

For dynamic coding and rapid prototyping I recommend to have a look at Lua. The engine is very small and easy to integrate in your c++ program.

3

The only way to do that in C++ is to unload the DLL with the code to be modified, modify the sources, invoke the compiler to regenerate the DLL, and reload the DLL. It's very, very heavy weight, and it only works if the compiler is present on the machines where the code is to be run. (Usually the case under Unix, rarely the case with Windows.)

Interpreted languages like Python are considerably more dynamic; Python has a built-in function to execute a string as Python code, for example. If you need dynamically modifiable code, I'd suggest embedding Python in your application, and using it for the dynamic parts.

0
0

Personally I have never played with re-compiling C++ during runtime, and I do not intend too. However I have been doing a lot of embedding of scripting languages lately.

Someone else mentioned the obvious first choice for embedding: Lua. Lua is a pretty popular language to embed and you will find a bunch of documentation about how to do it. Integrating it into the C++ will allow you to define behavior at runtime like you want.

What I am using is a wonderful langauge called Squirrel. It is a lot like Lua but with native object(class) support and C++-like syntax. I have managed to embed it into a C++ application, and through using the sqrat binding library both languages can share information easily.

I have squirrel building and initializing my UI. As a result, 0 compiling is required in order to craft and structure my UI for my game. I intend to take this a step further and use this gameplay-side in order to create similar behavior you are looking for(changing behavior at runtime through dynamic code usage)

I recommend Checking out squirrel here: http://www.squirrel-lang.org/

I plan on writing tutorials on how to embed squirrel and install the binding library, but I have not started on them yet. If I can remember, I will edit this post in the future (could be a few months) once I have completed them. In the meantime give it a shot yourself. You may find it to your liking.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.