EDIT: The question is flagged for similarity, but it is different as the file is being run in windows and "system()" is giving errors, unlike in the other question asked.
So, What I am trying to achieve is to compile and run a C program from within my C program.
- Why? I have used used fopen to create a file output.c and using fprintf, entered a simple code snippet into it. For instance a simple code snippet looks like this.
#include <stdio.h>
int main()
{
int a,b;
a=10;
scanf("%d", &b);
for(int i=0; i< 10 ; i=i+1)
{
b++;
}
printf("%d", b);
return 0;
}
This is an output file that is being generated, Now i want to run this code from within my original C code. Is it possible? It Yes, How?
I have tried using
system("gcc output.c -o output.out");
system("./output.out");
but it's giving me an error -->
undefined reference to `WinMain@16' collect2.exe: error: ld returned 1 exit status
For more context, What I am trying to do is open a file, convert it into C and find it's value, So, My approach so far has been to copy paste the content of the file into another file which I have created using:
followed by series of fprint.
Now, I am trying to execute that C file created! I am open to alternative method too! Thank You
Edit: This is the initial file
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *output;
output = fopen("experi.c", "w+");
fprintf(output, "#include <stdio.h>\n \n \nint main(){\n");
fprintf(output, "int a,b;\n");
fprintf(output, "a=10;\n");
fprintf(output, "scanf(");
fprintf(output, "\"%%");
fprintf(output, "d\"");
fprintf(output, ", &");
fprintf(output, "b");
fprintf(output, ");\n");
fprintf(output, "for(int i=0; i< 10 ; i=i+1)\n");
fprintf(output, "{\n");
fprintf(output, "b++;\n");
fprintf(output, "}\n");
fprintf(output, "printf(");
fprintf(output, "\"%%");
fprintf(output, "d\"");
fprintf(output, ", ");
fprintf(output, "b");
fprintf(output, ");\n");
fprintf(output, "return 0;\n");
fprintf(output, "}\n");
system("gcc experi.c -o a.out");
return 0;
}