Search code examples
cmingwmingw-w64

MinGW console application


Ok I've just started programming in C and I've found the MinGW compiler to be just what I need (easy to compile and run your application). I've written simple hello_world app and I just wanted it to print the result on the screen just to see if everything works fine. It doesn't output anything but it rather compiles it to .exe file and then when I run that file it prints the result. How to make a console application using MinGW. I've googled and read some documentation but can't quite get the answer.

This is the line i compile my code with: gcc hello_world.c

Edit:

#include <stdio.h>

int main() {
   int n = 10;
   printf("hello world %d\n",n);
   return 0;
}

I've tried using cmd and powershell but i don't think that's the issue.


Solution

  • You have created a console application.

    gcc hello_world.c
    

    asks gcc to compile and link hello_world.c into an executable, without telling it what you want the executable to be called. So it defaults to a.exe (on Windows) or a.out (on Unix/Linux).

    That a.exe is your console application. You can run it in the console (a.k.a shell, command-prompt) and, as you've seen, it does what you expect.

    If you would like the executable to have a particular name you have to tell gcc that, e.g.

    gcc -o hw hello_world.c
    

    -o is for "output". This will make you a program called hw.exe.

    I suspect also that you may think there is some command:

    gcc ??? hello_world.c
    

    That will compile, link and run your program. There isn't. Compilers don't run programs, they just make programs, that you can take away and run independently of the compiler.

    I recommend that you learn something about using gcc before you go any further. Here is a pretty good beginners' tutorial. And for definitive guidance, consult the official documentation