Years ago I found a interesting library called TinyCC, which provided a simple and small C compiler with one major twist… it can be embedded inside another application. Because of this, a program can compile new functions for itself at run time and call them using function pointers. In a sense it can be used like an immensely powerful scripting language, with the benefit that there is no speed hit due to interpretation or emulation. Its weakness as a scripting language is that you have to account for portability and security problems due to the execution of these ‘scripts’ running as native code on the host processor.
So far, I have used TinyCC for a few projects in the past and recently in a texture generator as well as my text adventure engine. The reason for this post however, is that TinyCC is not the most intuitive thing to embed into a windows application, so I wanted to take a moment to explain one clean method to do so.
There is no static library for use with visual C++ so that option is out, however since a fully compiled DLL file is included in the download package, it seemed that it would be easiest to dynamically link the DLL through code at run time. This requires loading the DLL and requesting all of the functions that are exported from it which is a fairly easy operation on any platform and generally differnt OS’s have much the same interface for doing so. The only tricky part is actually specifying all of the annoying function pointer casts correctly during binding.
Without farther talk, here is some straight forward code to load TinyCC and bind the interface for you. Which I hope it should all be fairly straight forward and easy to understand.