Last year, in university we used the library SDL to create a game from scratch. So I though of developping a game engine based on SDL. I will be using Qt to do the UI and SDL to implement the core of the engine (collisons, physics, game logic etc...). The game engine will be a simple application, for example the user creates a new project, adds a player, enemies, objectives... and then when he wants to test the game, the application will create the source code necessary for that game, compiles it and executes it. So my question is how can I compile that code and execute it ? Do I compile externally (with the help of commandes like gcc -c (use of system(), CreateProcess()) or is there another way ?
-
3You could write your own c++ compiler but I guess, thats not what you want. So yes, you call an external compiler. Or you use a interpreted language.– tkauslCommented Jun 10, 2016 at 1:23
-
5Compiling the code is the easiest part of what you're describing. You call the command line compiler, just like you would if you typed a command on the command line.– Robert HarveyCommented Jun 10, 2016 at 1:31
2 Answers
If you want to compile code within your program you could theoretically build off the source code for GCC. If you want to run the program within your program I guess you could try to compile it as a dynamic library and execute it that way. Unless you feel you have a good reason to do this it doesn't seem advantageous.
Some compilers may offer its facilities as a library not just a stand-along program.
For example TCC offers libtcc
, a library that can take C source code and compile it, and optionally run it. It's most of the internals of the tcc compiler stored as a library. However that specific example only compiles from C.
There is also libgccjit
, which is only alpha quality and requires you to assemble a data structure representing your program by a series of library calls to create variables, expressions, statements, etc, instead of parsing source code.