Jump to content

Can't compile c program with MinGW

Go to solution Solved by fizzlesticks,

The compiler uses the file extension in order to know how to compile the file. Your file ends with .txt instead of .c. You should turn off hiding file extensions for known file types then remove the .txt from the filename. To do that open any directory in Windows explorer and hit alt then go to tools -> folder options -> view and uncheck the hide extensions for known file types box.

Hey everybody,

 

I'm having a really hard time trying to compile a basic program written in C using MinGW and Windows 7 command prompt. The file is saved as "first.c" with notepad. 

#include <stdio.h>int main(void) {    int num;    num = 1;        printf ("I am a simple ");    printf ("computer. \n");    printf ("My favorite number is %d because it is first. \n", num);        getchar();        return 0;}

I have installed MinGW and set the environment variable Path for the user (not the system) to C:/MinGW/bin like I am supposed to.

 

When I change the directory to where the source code is located under my user documents, and try: "gcc first.c" I get:

 

"gcc: error:  first.c: No such file or directory

gcc: fatal error: no input files

compilation terminated"

 

Any help would be greatly appreciated!!

ASRock B550M PG RIPTIDE       Corsair Vengeance 16 GB DDR4             TEAMGROUP MP33 1 TB NVME SSD

AMD Ryzen 5 5600X                   Antec DF700 Case                                 MSI Radeon RX 580 4 GB ARMOR OC

 

Link to comment
Share on other sites

Link to post
Share on other sites

And how do you set the directory to where the source code is located?

Try to put your file in C disk. Then run "gcc C:/first.c"

Link to comment
Share on other sites

Link to post
Share on other sites

And how do you set the directory to where the source code is located?

Try to put your file in C disk. Then run "gcc C:/first.c"

 

Hmm, I tried that and I still got the exact same error message. This is tricky, I'm been having a hard time with it

ASRock B550M PG RIPTIDE       Corsair Vengeance 16 GB DDR4             TEAMGROUP MP33 1 TB NVME SSD

AMD Ryzen 5 5600X                   Antec DF700 Case                                 MSI Radeon RX 580 4 GB ARMOR OC

 

Link to comment
Share on other sites

Link to post
Share on other sites

I also tried running command prompt as administrator.

 

I put first.c in C directory and have tried:

 

"gcc C:/first.c"            (same error message)

 

"gcc C:/first.c.txt"        (file not recognized: File format not recognized....collect2.exe: error: ld returned 1 exit status 

 

"gcc first.c"                 (same error message as the first)

 

"gcc first.c.txt"            (same error message as the second)

 

 

Should I try setting the system Path environment variable for MinGW? I hear that's a bad thing that I shouldn't do, and that I should only set the User path environmental variable for MinGW. 

ASRock B550M PG RIPTIDE       Corsair Vengeance 16 GB DDR4             TEAMGROUP MP33 1 TB NVME SSD

AMD Ryzen 5 5600X                   Antec DF700 Case                                 MSI Radeon RX 580 4 GB ARMOR OC

 

Link to comment
Share on other sites

Link to post
Share on other sites

The compiler uses the file extension in order to know how to compile the file. Your file ends with .txt instead of .c. You should turn off hiding file extensions for known file types then remove the .txt from the filename. To do that open any directory in Windows explorer and hit alt then go to tools -> folder options -> view and uncheck the hide extensions for known file types box.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

The compiler uses the file extension in order to know how to compile the file. Your file ends with .txt instead of .c. You should turn off hiding file extensions for known file types then remove the .txt from the filename. To do that open any directory in Windows explorer and hit alt then go to tools -> folder options -> view and uncheck the hide extensions for known file types box.

 

Incidentally I just figured it out a few minutes ago but you hit the problem right on the head! I had been trying to compile first.c.txt this whole time. I only figured it out when I created duplicate source code named second.c that was actually saved as source code. What a headache. It actually took me a few days to figure it out. 

ASRock B550M PG RIPTIDE       Corsair Vengeance 16 GB DDR4             TEAMGROUP MP33 1 TB NVME SSD

AMD Ryzen 5 5600X                   Antec DF700 Case                                 MSI Radeon RX 580 4 GB ARMOR OC

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×