[c] How to compile C program on command line using MinGW?

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?

This question is related to c windows gcc command-line mingw

The answer is


It indicates it couldn't find gcc.exe.

I have a path environment variable set to where MinGW is installed

Maybe you haven't set the path correctly?

echo %path%

shows the path to gcc.exe? Otherwise, compilation is similar to Unix:

gcc filename.c -o filename

First:

Add your minGW's bin folder directory ( ex: C\mingw64\bin ) in System variables => Path. visual example


Compile:

.c: gcc filename.c -o desire

.cpp: g++ filename.cpp -o desire


Run:

desire/ or ./desire


make as usual... Just make sure you have it in your path and correct Makefile


Where is your gcc?

My gcc is in "C:\Program Files\CodeBlocks\MinGW\bin\".

"C:\Program Files\CodeBlocks\MinGW\bin\gcc" -c "foo.c"
"C:\Program Files\CodeBlocks\MinGW\bin\gcc" "foo.o" -o "foo 01.exe"

Just set the environment variable to the EXACT path to gcc.exe like this:

C:\Program Files (x86)\CodeBlocks\MinGW\bin\gcc.exe

I've had this problem and couldn't find why it kept happening. The reason is simple: Once you have set up the environment paths, you have to close the CMD window, and open it again for it be aware of new environment paths.


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.


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


You can permanently include the directory of the MinGW file, by clicking on My Computer, Properties, Advanced system settings, Environment variables, then edit and paste your directory.


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 encountered the same error message after unpacking MinGW archives to C:\MinGW and setting the path to environment variable as C:\MinGW\bin;.

When I try to compile I get this error!

gcc: error: CreateProcess: No such file or directory

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 desired hello.exe.

I ran this:

gcc hello.c -o hello

The result result was this (a blinking underscore):

_


Instead of setting the %PATH% you may enter your msys shell. In standard msys and mingw installation gcc is in path, so you can run gcc or which gcc.

I have a batch file sh.bat on my Windows 7, in %PATH%:

C:\lang\msys\bin\sh.exe --login %*

Whenever I want to use gcc I enter cmd, then sh, then gcc. I find it very convenient.

When working with linux originated software avoid spaced directories like Program Files. Install them rather to Program_Files. The same regards to tools that you may want to run from msys environment.


I am quite late answering this question (5 years to be exact) but I hope this helps someone.

I suspect that this error is because of the environment variables instead of GCC. When you set a new environment variable you need to open a new Command Prompt! This is the issue 90% of the time (when I first downloaded GCC I was stuck with this for 3 hours!) If this isn't the case, you probably haven't set the environment variables properly or you are in a folder with spaces in the name.

Once you have GCC working, it can be a hassle to compile and delete every time. If you don't want to install a full ide and already have python installed, try this github project: https://github.com/sophiadm/notepad-is-effort It is a small IDE written with tkinter in python. You can just copy the source code and save it as a .py file


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:

E:\proj> gcc -c fact.c

Now I had an object file of fact.c (fact.o). after that:

E:\proj>gcc -o prog.exe fact.o main.c

Then my program (prog.exe) was ready to use and worked properly. I think that -c after gcc 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.


Examples related to c

conflicting types for 'outchar' Can't compile C program on a Mac after upgrade to Mojave Program to find largest and second largest number in array Prime numbers between 1 to 100 in C Programming Language In c, in bool, true == 1 and false == 0? How I can print to stderr in C? Visual Studio Code includePath "error: assignment to expression with array type error" when I assign a struct field (C) Compiling an application for use in highly radioactive environments How can you print multiple variables inside a string using printf?

Examples related to windows

"Permission Denied" trying to run Python on Windows 10 A fatal error occurred while creating a TLS client credential. The internal error state is 10013 How to install OpenJDK 11 on Windows? I can't install pyaudio on Windows? How to solve "error: Microsoft Visual C++ 14.0 is required."? git clone: Authentication failed for <URL> How to avoid the "Windows Defender SmartScreen prevented an unrecognized app from starting warning" XCOPY: Overwrite all without prompt in BATCH Laravel 5 show ErrorException file_put_contents failed to open stream: No such file or directory how to open Jupyter notebook in chrome on windows Tensorflow import error: No module named 'tensorflow'

Examples related to gcc

Can't compile C program on a Mac after upgrade to Mojave Compiling an application for use in highly radioactive environments Make Error 127 when running trying to compile code How to Install gcc 5.3 with yum on CentOS 7.2? How does one set up the Visual Studio Code compiler/debugger to GCC? How do I set up CLion to compile and run? CMake error at CMakeLists.txt:30 (project): No CMAKE_C_COMPILER could be found How to printf a 64-bit integer as hex? Differences between arm64 and aarch64 Fatal error: iostream: No such file or directory in compiling C program using GCC

Examples related to command-line

Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) Flutter command not found Angular - ng: command not found how to run python files in windows command prompt? How to run .NET Core console app from the command line Copy Paste in Bash on Ubuntu on Windows How to find which version of TensorFlow is installed in my system? How to install JQ on Mac by command-line? Python not working in the command line of git bash Run function in script from command line (Node JS)

Examples related to mingw

MINGW64 "make build" error: "bash: make: command not found" How to install MinGW-w64 and MSYS2? printf, wprintf, %s, %S, %ls, char* and wchar*: Errors not announced by a compiler warning? mingw-w64 threads: posix vs win32 Serial Port (RS -232) Connection in C++ What is difference between sjlj vs dwarf vs seh? Unable to specify the compiler with CMake to_string is not a member of std, says g++ (mingw) How to compile makefile using MinGW? Getting started with OpenCV 2.4 and MinGW on Windows 7