-2

I'm making an application that will create a working executable based on what the end-user inputs into the program.

For example:

if (make_annoying_sounds == true)
{
     //Generates an executable that makes annoying beeping sounds
}
else
{
    //Generates an executable that doesn't make annoying beeping sounds
}

Basically I want my program to generate/create another program. I've seen/used many programs that do this. I have searched all over the internet and can't find anything. All help is appreciated. (Create a program, from within my program).

Sedulous
  • 55
  • 1
  • 1
  • 5

4 Answers4

1

Try using a basic system call to invoke a compiler after you've created the source file.

You can create the source file with just the utilities found in stdio.h

Security Note: The system function is known to be dangerous. When in doubt, call a function like exec to invoke the compiler. Although exec erases the currently running process, so you should use fork and then call exec if you want to keep doing stuff after the compilation has finished.

randomusername
  • 7,927
  • 23
  • 50
  • Thank you, this should work fine. Are there any other ways of doing this? Nonetheless, this should work anyway. EDIT: Nevermind, thank you! – Sedulous Jan 11 '14 at 17:49
  • @Sedulous Unless you want to implement the compilation routines yourself, which is a lot of work, or you want to call the compiler's private API, which is very unreliable as the API can change without notice, it's better to just create a separate process. – randomusername Jan 11 '14 at 17:53
1

So you want to create a compiler? This question below contains a whole list of resources to help you get started.

Learning to write a compiler

Community
  • 1
  • 1
Hans
  • 343
  • 1
  • 4
  • 11
0

You need to do the following:

  1. Based on the user input, generate the code for the custom program.
  2. Compile that code into an executable file.
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
0

Theorically, you could, depending on what the user inputs, make your C code generate C-code inside your if statements. However this would be quite difficult.

The best way I think is to make an independent C engine which will only implement functions that any of the generated program can execute (playing the sound given in parameter, for example). The program you are trying to code (not the engine, but the one with the if statements, let's call it the "master program") must generate code which implements the algorithm which will choose what function of the engine to call and when. This generated code should be written in a scripting language like lua, since in is easier to generate script code than C. Thus, the engine should be designed to be able to communicate with Lua scripts. When the user clicks on the final "generate program" button of the master program, the master program calls gcc to compile the engine and the Lua script to generate the program the user tried to create. This is long, but this is, I think, the right way to do it.

Vulpo
  • 873
  • 1
  • 9
  • 25