How to compile C program on command line using MinGW?

前端 未结 14 1100
天命终不由人
天命终不由人 2021-01-30 17:19

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

相关标签:
14条回答
  • 2021-01-30 17:46

    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.

    0 讨论(0)
  • 2021-01-30 17:49

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

    C:\Program Files (x86)\CodeBlocks\MinGW\bin\gcc.exe
    
    0 讨论(0)
  • 2021-01-30 17:49

    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

    0 讨论(0)
  • 2021-01-30 17:49

    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

    0 讨论(0)
  • 2021-01-30 17:52

    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.

    0 讨论(0)
  • 2021-01-30 17:53

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

    _

    0 讨论(0)
提交回复
热议问题