0

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
Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
  • 8
    ***It should be "subject.exe"*** No it should not. `-o name` specifies the output file name. By historical reasons a.exe / a.out is the default when you did not give your compiler an output file name. – drescherjm Jan 03 '23 at 16:19
  • 3
    Welcome to SO. Normally the output file is called `a.out` on Linux-like systems and `a.exe` on Windows systems. If you want to change this, use `-o ` option. – Gerhardh Jan 03 '23 at 16:19
  • 4
    As you can provide multiple source files and libraries to `gcc` command, picking the name of a source file might not be a very useful decision except for the tinyest of tiny programs. – Gerhardh Jan 03 '23 at 16:20
  • but is there any way to automatically compile it to the source file name with .exe ? – Ehteshamul Haque Jan 03 '23 at 16:26
  • 1
    Per an earlier comment, what logic would you put in place for multi-file builds? Toy programs shouldn't get special treatment is the gist of what's being said. – sweenish Jan 03 '23 at 16:28
  • You could write a script to do that or just type a few letters more on the command line and use the `-o name` option. – drescherjm Jan 03 '23 at 16:28
  • 2
    `gcc -o subject.exe subject.c`,, even in a world where coders try to do as little work as possible that's hardly an excessive amount of typing. If that's still too much for you, the history of your terminal (eg, cmd or powershell) can be accessed with the up arrow, or you can put that command in a batch file and simply run that. Or you can use a build system like cmake that will generate build files for you. – yano Jan 03 '23 at 16:29
  • 3
    From `gcc -?` : `-o Write output to `. To answer your question about automatic calculation of output file from input file, no, there isn't, if for no other reason than because you can specify multiple input files on the same command. E.g. `gcc a.c b.c c.c` . Which input file did you have in mind? – WhozCraig Jan 03 '23 at 16:29
  • It is far easier to write a program that does not assume what the output file name should be, and compilers are already pretty darn complicated. – user4581301 Jan 03 '23 at 16:33
  • Does this answer your question? [Determining C executable name](https://stackoverflow.com/questions/653807/determining-c-executable-name) – Alan Birtles Jan 03 '23 at 16:52

2 Answers2

3

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

ikegami
  • 367,544
  • 15
  • 269
  • 518
3

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.

Willis Hershey
  • 1,520
  • 4
  • 22
  • Re "*but because programs can be compiled from multiple source files*", This is not an issue. It could be named after the first or the last. It's *only* for historical and backwards-compatibility reasons that `a.out` is always used. – ikegami Jan 03 '23 at 17:49