The Pointer The Pointer -4 years ago 110
C Question

Why does this incorrect compilation and linkage result in a deletion of my C source file?

I have noticed that an erroneous compilation and linkage command results in the deletion of one of my C source files.

Assume I have 2 source files and a header file:

, and
. I then erroneously attempt to compile and link these files with the shell command
cc -std=c99 -Wall -Werror -pedantic -o test1.c test2.c

Due to this erroneous command, I get the error message:

Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64

After this, the file
is deleted.

I am curious as to what's causing the deletion and why?

Thank you.

Answer Source

Your command is outputting to test1.c, the command fails and therefore effectively deletes the file.

cc -std=c99 -Wall -Werror -pedantic -o output test1.c test2.c
                                     ^ the output flag

I've modified your command to output the program to ./output.

[Edited due to further question in comments]

To the question of why cc deletes the file: I ran strace -f cc -o test1.c test2.c to find out more about what it was doing. At some point in the output of strace you see this line:

[pid XXXXX] unlink("test1.c")           = 0

unlink will delete a file, read more at man 3 unlink.

I believe the reason cc deletes the file is cc thinks it is creator of the file, and therefore is trying to clean up after itself.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download