11

I am new to C programming. When I tried to compile it, I got an a.out file (I don't know where it came from) and it somehow disappeared. What is happening here?

Here are the commands I ran:

$ gcc hello.c
$ ls
a.out hello hello.c

$ a.out
a.out: command not found

$ gcc a.out
a.out: file not recognized: File truncated
collect2: error: ld returned 1 exit status

$ ./hello
Hello, world!$ ./a.out
bash: a.out: No such file or directory

$ ls
hello hello.c
hgr
  • 178
  • 1
  • 1
  • 10
  • 8
    `a.out` is the default name given to the built executable. You can change it with the appropriate command-line option for your C compiler. – user2864740 Jan 27 '17 at 17:57
  • As for why it's disappearing, we can only guess (which is bad, and makes your question off-topic btw), but I've had cases where an overprotective antivirus would do that. – Siguza Jan 27 '17 at 17:59
  • This `a.out: file not recognized: File truncated` is done by `gcc` itself. No mistery there. – alvits Jan 30 '17 at 23:11
  • This must be a mega dupe. This is one of the first things a beginner runs into when compiling a C program on Unix-like systems (or only with older versions of compilers?). – Peter Mortensen Feb 19 '22 at 11:02
  • *a.out* is claimed to have been [deprecated since the early 2000s](https://stackoverflow.com/questions/8303536/generating-a-out-file-format-with-gcc/8303623#8303623). Is this in [a Turbo C++ environment](https://www.quora.com/What-are-some-bitter-truths-about-engineering-in-India/answer/Adhokshaj-Mishra)? – Peter Mortensen Feb 19 '22 at 11:54
  • *"a.out is a* ***file format*** *used in* ***older*** *versions of Unix-like computer operating systems for executables, object code, and, in later systems, shared libraries."* ([ref](https://en.wikipedia.org/wiki/A.out). My emphasis). Though the ***file name*** "a.out" may still be used. – Peter Mortensen Feb 19 '22 at 15:14

3 Answers3

28

a.out is the default executable name generated by the gcc. Once you invoke gcc a.out (which you really shouldn't - as it is passing a.out as an input to gcc), it is trying to create a new a.out, but failing as it is trying to read the existing a.out as a source file, which it is not. This is what making it "disappear".

If you want gcc to generate an executable with specific name, use the -o switch followed with the desired name:

gcc hello.c -o hello

will generate the executable named hello, which can be run using

./hello
Eugene Sh.
  • 17,802
  • 8
  • 40
  • 61
4

a.out is the default executable, since you didn't tell GCC what to name it (by using -o). Just typing a.out won't run it, because it's in the current directory, and Linux doesn't put the current directory in the path like other broken OSes do, so you have to type ./a.out to run it.

You later typed gcc a.out, which erased it, and tells gcc to try to compile the executable as if it were a C program, which makes no sense, so the a.out it produced is empty.

What you meant to do was:

gcc hello.c
./a.out

Or even better:

gcc -o hello hello.c
./hello
Lee Daniel Crocker
  • 12,927
  • 3
  • 29
  • 55
2

In addition to the previous answer, I'd suggest you use

gcc -o hello hello.c

This will create an executable called "hello" rather than the default "a.out". Of course you can call it whatever you want.

Jon Z.
  • 121
  • 3
  • 8