I use gcc-compiler at Ubuntu 18.04. To compile and run a program I use command:
gcc program.c -o program.exe && ./program.exe
So, how can I automatically delete this program.exe file after program finishes work?
I use gcc-compiler at Ubuntu 18.04. To compile and run a program I use command:
gcc program.c -o program.exe && ./program.exe
So, how can I automatically delete this program.exe file after program finishes work?
That should do the trick:
gcc program.c -o program.exe && ./program.exe && rm program.exe
Please nothing that usually binary programs in Linux doesn't have the .exe
extension (but nothing prevents you from putting it, it's a just a little misleading).
Your rm
could be some shell alias. Then you could force the real rm
program to run by giving its (standard) path /bin/rm
(instead of just rm
in the command line above).
On Linux and Unix systems, an executable is allowed to remove itself when running (using remove(3) or unlink(2)). I gave an example here. But this is not common practice (and could confuse your user).
So you might code your C program with an explicit unlink
doing that. This is unusual, and you should document that you are doing so.
Most executables are somehow permanent and are expected to be reusable a lot of times. You need to document why you don't want such a common behavior (and explain precisely why, when and how should the executable be deleted).
And I recommend compiling your executable with all warnings and debug info, so with gcc -Wall -g program.c -o program
(later you could ask for optimization by adding -O2
). This would ease debugging.
Of course, if you just want to remove an executable after it finishes running completely, you can simply use rm(1) (as explained by blue112's answer). You should explain why (and when) you want to do so (running rm program
just after execution of ./program
is unusual).