What command does one have to enter at the command line in Windows 7 to compile a basic C program?
Like I am literally wondering what you type in the command prompt, to compile a .c file.
I tried:
> gcc foo.c
But it says:
'gcc' is not recognized as an internal or external command,
operable program or batch file.
I also tried:
> minGW foo.c
But I got back:
'minGW' is not recognized as an internal or external command,
operable program or batch file.
I have a path environment variable set to where MinGW is installed:
C:\Program Files (x86)\CodeBlocks\MinGW\bin
I can't really find any information on where I'm going wrong, and can't find anything in the official MinGW documentation, as it seems like this is something so simple, sort of an embarrassing question, that it's figured people know what to do?
If you pasted your text into the path variable and added a whitespace before the semicolon, you should delete that and add a backslash at the end of the directory (;C:\Program Files (x86)\CodeBlocks\MinGW\bin
I encountered the same error message after unpacking MinGW archives to
C:\MinGW
and setting the path to environment variable asC:\MinGW\bin;
.When I try to compile I get this error!
I finally figured out that some of the downloaded archives were reported broken while unpaking them to
C:\MinGW
(yet I ignored this initially). Once I deleted the broken files and re-downloaded the whole archives again from SourceForge, unpacked them to C:\MinGW successfully the error was gone, and the compiler worked fine and output my desiredhello.exe
.I ran this:
The result result was this (a blinking underscore):
_
I once had this kind of problem installing MinGW to work in Windows, even after I added the right System PATH in my Environment Variables.
After days of misery, I finally stumbled on a thread that recommended uninstalling the original MinGW compiler and deleting the
C:\MinGW
folder and installing TDM-GCC MinGW compiler which can be found here.You have options of choosing a 64/32-bit installer from the download page, and it creates the environment path variables for you too.
I had the same problem with .c files that contained functions (not
main()
of my program). For example, my header files were "fact.h" and "fact.c", and my main program was "main.c" so my commands were like this:Now I had an object file of fact.c (fact.o). after that:
Then my program (prog.exe) was ready to use and worked properly. I think that
-c
aftergcc
was important, because it makes object files that can attach to make the program we need. Without using-c
, gcc ties to find main in your program and when it doesn't find it, it gives you this error.In Windows 10, similar steps can be followed in other versions of windows.
Right Click on "My Computer" select Properties, Goto Advanced System Settings -> Advanced -> Select "Environment Variables.." .
Find "Path" select it and choose edit option -> Click on New and add "C:\MinGW\bin" (or the location of gcc.exe, if you have installed at some other location) -> Save and restart command prompt. Gcc should work.
Instead of setting the
%PATH%
you may enter your msys shell. In standard msys and mingw installation gcc is in path, so you can rungcc
orwhich gcc
.I have a batch file
sh.bat
on my Windows 7, in %PATH%:Whenever I want to use gcc I enter cmd, then
sh
, thengcc
. I find it very convenient.When working with linux originated software avoid spaced directories like
Program Files
. Install them rather toProgram_Files
. The same regards to tools that you may want to run from msys environment.