cwindowsgcccmd

.exe file is not being created in Windows for C by cmd by GCC compiler


I am also worried about how to make you understand about my problem and share my experience. I am learning C. I used before the Coding Blocks IDE to compile and run C code. I learned that as a beginner it would better to use cmd to compile and run code than an IDE. I am using Windows 8. I was not used with the Windows command prompt. I also learned that to be an advanced user and good programmer, I should know the use of command line use as well.

After setting the environment variable started to compile C code by cmd by the following command:

gcc -o "executable file name" "C source code file name"

It creates a .exe file. But no .o file. When I worked with IDE, also a .o file was created in same directory as the .exe file.

1. What is the possible reason for it?

After compiling the code, I ran it by calling "executable file name".
And when checked the directory, then a .exe file was found. But now the problem is, recently I am compiling and running the code, I found that there is no .exe file created, but a file has created with unknown extension "POINTERWHILE File (.pointerWhile)" (copied from file properties). This unknown extension file is only running by the cmd. It may be the one part of my source code file name. The code has given below and the name of source code.

Name of source code: Example184.1.pointerWhile

#include<stdio.h>
main()
 {
  char *p = "stop";
  char str[80];
   do
    {
     printf("Enter a String: ");
     gets(str);
    }
  while(strcmp(p,str));
 }

This program will take input until stop is entered.

This kind of things happened for several other files. Then I copy them to another folder and compile and run, but the same result.

I compiled and run the same code form same folder and other folder by Coding Blocks, the result is the same. The file is only running when it is in IDE.

Then I make another file with other name in same directory and other, but the same code. Then it is creating a .exe file.

2. What may the cause for creating .exe file?


Solution

  • Great effort from you in learning the cmd along with C!

    However, I think you're confusing the extension (name) of the file, and its contents. When you tell gcc to generate an executable file, you might want to give it a .exe extension, just like you said, assuming your source is called "myprog.c":

    gcc -o myprog.exe myprog.c
    

    This should always generate a myprog.exe file. But nothing prevents you from calling it like this: gcc -o myprog.myFancyExtension myprog.c

    You should still be able to run the program via the cmd (I didn't test that on windows though). Note that on other systems, like Linux, executable files usually have no extension at all.

    Also, you're talking about how your IDE generates a myprog.o object file along with the myprog.exe executable file. To do this, your IDE is calling the compiler (gcc) twice: once to generate the .o file, and another time to generate the .exe file. There is no way to generate both files in one run of the compiler. The advantage of learning the cmd is that you will learn what your IDE is actually doing for you without telling you. The drawback is that you have to actually learn it ;)

    Usually, people do not like to use gcc by hand, because as your projects grow, they will use different files and it becomes tedious to recompile them all manually. To overcome this, you will want to use a build tool, such as Make. I'm not familiar with development under Windows, so I can't recommend any specific tool. The point of those tools is that when your project has multiple source files, you only have to call the builder once, like this for Make:

    make
    

    instead of manually calling:

    gcc -c myFeature1.c
    gcc -c myOtherFeature.c
    gcc -c myYetAnotherFeature.c
    gcc -c myMain.c
    gcc -o myMain.exe MyMain.o myFeature1.o myOtherFeature.o myYetAnotherFeature.o
    

    Hope it helps