XO39
XO39

Reputation: 483

How to run C code/file with MINGW?

OK, I just began learning C language, and I read a lot of people suggest to use MinGW as a compiler. I downloaded, installed it and set it correctly (I think) and set the path. everything seems to be OK. My problem is that I can't find a way to run a C file from the CMD, when I do something like this:

C:\>gcc a\c1.c

nothing appear on the CMD

In c1.c file I have the following:

#include <stdio.h>

int main(void)
{
    printf("this is a test.\n");
    return 0;
}

I tried tcc compiler and when using it this way:

C:\>tcc a\c1.c

it outputs the following:

this is a test.

How can I make MinGW output that the same way? I looked in the MinGW/gcc manual and HowTos and couldn't find a way to do the same thing.

Upvotes: 3

Views: 22548

Answers (3)

Keith Thompson
Keith Thompson

Reputation: 263197

gcc filename.c compiles and links your file -- but it doesn't write the executable to hello.exe as you might expect (and of course it doesn't execute it).

And, as is the convention for Unix-based tools, gcc generally doesn't print anything unless something went wrong. The fact that it finished and you got a new C:\> prompt is what tells you that it succeeded.

On Unix, for historical reasons, the default name of an executable is a.out (it stands for "assembler output"). For MinGW, the default name is a.exe, since Windows requires a .exe suffix for executable files.

So after running gcc a\c1.c, you can run a.exe or .\a.exe to run the program.

Or, better, as Doug suggests, you can use the -o option to specify the name of the generated executable file.

(If you want to run just the compiler and generate an object file rather than an executable, use gcc -c.)

Note that this behavior (not printing anything unless there's a problem, naming the executable file a.out or a.exe, etc.) is defined by the gcc compiler in particular. It's not a feature of the C language. Other C compilers might or might not behave similarly.

Upvotes: 9

Since you are a newbie, I strongly suggest you to compile with the -Wall flag (to get all warnings GCC can give you) and with the -g flag (to get a debuggable executable). Take the habit to improve your code till you get no warnings any more. And indeed, learn how to use make. If you want the compiler to optimize the generated code, replace -g with -O2 (but do that once the program is correctly running, and you have debugged it).

Upvotes: 1

Doug Currie
Doug Currie

Reputation: 41170

tcc is compiling, linking, and executing your C file.

gcc is compiling your C file, but not linking or executing it.

Try:

C:\>gcc -o a\ci.exe a\c1.c

Followed by

a\ci.exe

Once you have that figured out, you will want to learn about make

Upvotes: 1

Related Questions