Home > Mobile >  Why gcc compiler giving the complied file a new name?
Why gcc compiler giving the complied file a new name?

Time:01-04

I have reinstalled mingw in my system and downloaded the gcc compiler.

I was shocked after compiling the first file which was "subject.c" but the name of the compiled file which gcc returned was "a.exe". It should be "subject.exe" but do not know why this happened.

Can anyone please explain the reason behind this ?

expected:

gcc subject.c
ls
subject.c subject.exe

tried:

gcc subject.c
ls
subject.c a.exe

CodePudding user response:

-o can be used to give the name of the output file.

For example,

gcc -Wall -Wextra -pedantic subject.c -o subject.exe

(Do enable your compiler's warnings!)

CodePudding user response:

gcc names its output files, in the absence of other instructions, a.out or a.exe depending on system environment because that is what it's supposed to do.

To override this default behavior, you can use the -o flag which tells gcc that the next argument is the desired name for the output file. For instance:

gcc -o subject.exe subject.c

There is no automatic functionality built into gcc to strip a source file of its file extension and add .exe to the end but this can be done manually with Makefiles or other similar scripts, for instance you can write a Makefile with the following contents:

%.exe: %.c
    gcc -o $@ $<

Then a command like make subject.exe would be translated to gcc -o subject.exe subject.c, which may be what you're looking for.

There is functionality built into gcc to strip source files of their extensions during different parts of the compilation process, which may have been what confused you. For instance a call like gcc -c subject.c can be expected to produce an object file called subject.o, likewise gcc -S subject.c can be expected to produce an assembly language file called subject.s, however this does not apply to executable files not only for historical reasons, but because programs can be compiled from multiple source files and there is not always a clear way to choose a name for the executable output.

  • Related