Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is this a.out and what makes it disappear?

Tags:

c

gcc

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
like image 274
hgr Avatar asked Nov 30 '22 22:11

hgr


1 Answers

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
like image 102
Eugene Sh. Avatar answered Dec 04 '22 13:12

Eugene Sh.