简体   繁体   中英

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?

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

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.

只需将环境变量设置为gcc.exe的EXACT路径,如下所示:

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

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):

_

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.

您可以永久包含MinGW文件的目录,方法是单击“我的电脑”,“属性”,“高级系统设置”,“环境变量”,然后编辑并粘贴目录。

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

像往常一样......只要确保你有它在你的路径和正确的Makefile

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.

如果将文本粘贴到路径变量并在分号前添加了空格,则应删除该文本并在目录末尾添加反斜杠(; C:\\ Program Files(x86)\\ CodeBlocks \\ MinGW \\ bin

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.

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"

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM