Not logged in, Join Here! or Log In Below:  
News Articles Search    

 Home / General Programming / Is it possible to generate compiled code on the fly? Account Manager
Archive Notice: This thread is old and no longer active. It is here for reference purposes. This thread was created on an older version of the flipcode forums, before the site closed in 2005. Please keep that in mind as you view this thread, as many of the topics and opinions may be outdated.
James Fingleton

May 18, 2005, 05:02 PM

Hi everyone,

In my 3d engine I would like to have some form of scripting. I've tried Lua, but then had the idea of using a C/C++ compiler to compile (very simple) files that can then be ran in the same thread as the 3d engine. You would then register some "glue" functions between the engine and the just compiled object code and then be able to add symbols that tell it about the 3d engine glue functions.

There's already TinyC:

that does what I described, but it's not too well documented and I'm failing to get it to work.

Is it possible to use a common c/c++ compiler such as gcc to do the compiling? Then you would read in the generate object file into a buffer in the engine, and somehow execute it?

Thanks for any help/suggestions.


May 18, 2005, 05:34 PM


You could use the microsoft C++ compiler (which is free) or GCC in command line to compile a dll that you would load dynamically. The problem of doing so is that you will lose the ability to Debug your scripts and to track errors. The huge drawback of that is that as C++ code can do anything, link any library and do everything in the application's memory space it can compromize the behaviour of your engine. Enven worst if it is aimed at creating a modder community you cannot be sure that some malicious code hasn't been integrated to your engine. Scripting by definition runs in a sandbox not on the CPU. You can always put your game logic into a dll (and it is done this way in a lot of engines) but I really don't recommand to use run time compilation for this kind of bytecode.

As an alternative, if your project is targetted at a microsoft environment, you can use C# (or any .NET language) from within your C++ code as a scripting language. You will have better performances than (LUA) wich bytecode is interpreted because .NET assemblies are Just-In-Time compiled to native bytecode. There is a sample on how to do that in the directX SDK (though it's also applicable to an openGl engine). The example shows how to enforce classical security policies (script cannot access the hard drive for exemple).



May 27, 2005, 11:02 AM

This one is designed for on-the-fly compilation, it even allows C programs to be run like shell scripts:

Or really just use a scripting language, there are a few really good ones, Angelscript, for example. If I understand right what you want, then Angelscript will do just fine.

Fabian 'ryg' Giesen

May 27, 2005, 11:32 AM

Errr, damon, would you mind reading more than the topic and the first half of the first sentence before replying? This is ridiculous.


May 28, 2005, 04:41 AM

I already made such a system before, it loaded .obj files and linked them dynamically. It isn't very hard to write, you just need a good description of the obj file format.

The disadvantage I encountered was the nasty hacked code it required, like tons of pointers to member functions as "exports" from the engine and such. I also remember that there was a problem with constructors and calling conventions, but I can't remember it exactly. For some odd reason I used borload c++ to generate the .obj files and ms vc++ to load them, which didn't make things easier. Later I switched to ms vc++ for the .obj files too.

Besides the nasty code I can really recommend such a system, short development time, fast runtime.


May 28, 2005, 05:58 AM

Why not use DLLs? thats what they're intented for


May 28, 2005, 06:43 AM

DLL's are intended for sharing common code, not for instancing 1000's of actors/scripted things.


May 28, 2005, 07:07 AM

What I mean is why hack together dynamic linking with obj files when dynamic link libraries (ie collections of objects) provide the facilities for you

This thread contains 8 messages.
Hosting by Solid Eight Studios, maker of PhotoTangler Collage Maker.