0

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;
}
  • 1
    Those two `system` calls are exactly what I was about to suggest. They should/could/would work. If they're not working for you, it probably has to do with the fact that Windows is not Linux. The gyrations that mingw goes through (are you using mingw?) to simulate a Linux environment aren't perfect, and you may have stumbled into one of the limitations. – Steve Summit Dec 12 '22 at 18:16
  • 1
    _Caveat:_ I'm not too familiar with windows. But, AFAICT, `gcc` is trying to create a GUI program. See: [undefined reference to `WinMain@16'](https://stackoverflow.com/q/5259714/5382650) – Craig Estey Dec 12 '22 at 18:17
  • Rather than trying to describe the "parent" executable, it would be better to post its code. Also (WRT the "more context"), after you open the file, you aren't *converting the file (or code snippet) to C* - as it is already written in C - you are compiling it (presumably). It is hard to tell what you mean exactly without the intial code. – Greenonline Dec 12 '22 at 18:18
  • 2
    If you enter that command `gcc output.c -o output.out` in your command line, does it work as expected? – Gerhardh Dec 12 '22 at 18:30
  • Since your program uses `scanf` to read input, it's going to be a little bit weird/awkkard to run it using `system(./output.out);`. When it runs, it's going to try to read from standard input — which *might* work, or it might cause you additional headaches. I'd recommend working with a less interactive program when doing this sort of thing — that is, have the progam work on data that's either compiled in, or taken from the command line (`argv`), not read in from standard input using `scanf`. – Steve Summit Dec 12 '22 at 18:36
  • Edit: I have added the initital code too, I wanted to add the main file but that's a complex code so I simulated a similar problem with another file – Account101 Dec 12 '22 at 18:52
  • Another way to look at the question is, I have a very basic interactive C file, whose output I want to display on my console. – Account101 Dec 12 '22 at 18:55
  • 1
    The problem may simply be that you're not calling `fclose` after writing `experi.c` and before trying to compile it. – Steve Summit Dec 12 '22 at 18:57
  • Is it somehow possible to get my .c file in my original code and evaluate it in this code itself? – Account101 Dec 12 '22 at 18:58
  • 1
    There are embedded C interpreters that might allow you to do that, but they're rare and complex beasts. The approach you're taking is the one that's generally recommended. Do try adding the `fclose` call. – Steve Summit Dec 12 '22 at 18:58
  • 1
    Hey Guys! The question is solved, The problem was that I didn't include the fclose. Thank You So Much!! – Account101 Dec 12 '22 at 19:05

1 Answers1

1

Output written to C's stdio streams (such as output in your code) is, by design, buffered. At any given time some/all of the output may be sitting in an internal stream buffer, not yet written out to the file on disk.

But it's the file on disk that the C compiler is going to see when you invoke

system("gcc experi.c -o a.out");

So, before calling system, you need to call fclose to indicate that you're done writing the file. Among other things, this will cause the stdio package to properly flush all buffered output to the on-disk file:

fclose(output);

You can also force the output buffer to be written to disk by calling fflush, and this leaves the file stream open so that you can write more to it later, which is sometimes useful, though probably not what you want here.

Steve Summit
  • 45,437
  • 7
  • 70
  • 103