Reputation: 3
I am making a program in c that can produce another c code.
How to, using the first program, compile and run the second program immediately after the second program has been produced?
Upvotes: 0
Views: 214
Reputation: 12910
I'll make two notes in one:
First, if you have one program that generates source code, why not use the normal build system to handle this for you? For a Make-based build system it could look something like this:
second_program : second.c
$(CC) $(CFLAGS) -o $@ $<
second.c : first_program
./first_program $(GENERATION_OPTIONS) > $@
first_program : $(LIST_OF_SOURCE_FILES)
$(CC) $(CFLAGS) -o $@ $<
This would be more in line with the Unix philosophy than having the first program run an external command, which is always nice.
Second, do you want the second program to be generated and executed dynamically? I.e. will the resulting code depend on some dynamic state of the first program, and could the output from the second program be relevant to the first? If so, perhaps you should take a look at what you can do with a library to run some script language like LUA or ECMAScript. (This is perhaps a bit too advanced for the case you are asking about, but it's always nice to know what options there are.)
Upvotes: 2
Reputation: 26171
You'd need to embed a compiler into your app(such as libtcc or lcc) or invoke one via a command line, but that requires detecting what the user has installed or at the very least including extra binaries with your app
Upvotes: 1
Reputation:
One way is to used system()
call ...
system("cl generated_file.c -o gen_exe") ;
system("./gen.exe");
or
system("gcc generated_file.c -o gen.exe");
system("./gen.exe");
Or you use a simle batch or script or makefile to do this
Upvotes: 6