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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With