Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to run C program through cmd

Tags:

c

cmd

Hello everyone I'm learning C and am trying to figure out how to run it through the command console cmd. I have eclipse installed along with Mingw and added these to the path:

C:\MinGW\bin\;C:\MinGW\msys\1.0\bin

I wrote this program on notepad++ for a quick test run and save it to C:\test.c and also under a folder C:\Users\Pikachu\Music\C code while I was trying to figure it out:

#include <stdio.h>

int main()
{
    printf("Hey, Buddy!\n");
    return 0;
}

On the cmd console I typed:

c:\>gcc test.c 

and got the error message:

c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/id.exe: cannot ope
n output file a.exe: Invalid argument
collect2.exe: error: ld returned 1 exit status

When I typed:

c:\>cd c:\Users\Pikachu\Music\c code

and then:

gcc test.c

it just skipped a line as if nothing happened and went back to square one:

c:\Users\Pikachu\Music\c code>gcc test.c

c:\Users\Pikachu\Music\c code>

I was wondering if anyone knows what's going on and could help me out, I'd be so happy if I could see "Hey, Buddy" from cmd! Does anyone also know why I get the error message running it from c:\ and nothing when I run it from the Music\c code\test.c folder even though I'm supposedly running the same file test.c?

I've tried searching around and have picked up references on how the computer can't link to the proper dll's however I'm not sure how to implement this to my specific problem.

Oh and curiously enough when I tried to save another file in c:\ I got a message saying that I didn't have permission to do that even though 5 minutes prior I had done just this. Any insights?

Thanks for your help!

like image 664
000 Avatar asked Oct 01 '22 12:10

000


1 Answers

When you run gcc on your C source file, all that it will do it generate an executable file. I believe its called a.exe by default but I would recommend naming it with the -o option:

gcc text.c -o test.exe

Once your file is successfully compiled, run the executable to say hello to the worls:

c:\Users\Pikachu\Music\c code> .\test.exe

As for the first error you got, maybe it has to do with gcc not being able to create the output executable on the root c: folder. I would recommend doing your coding some folder your user owns instead of on a system folder for this reason.


By the way, gcc supports many other options. I highly recommend using -Wall to turn on warnings and choosing what version of the C standard to follow (-std=c99 or -ansi, together with the -pedantic flag).

like image 109
hugomg Avatar answered Oct 05 '22 08:10

hugomg