简体   繁体   中英

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

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

Here are the commands I run:

$ 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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM