How can I recover source overwritten by a bad compile command?
I had written a source code of C++ and complied it with the same name using the following command line.
For example:
c++ source-code.cpp -o source-code.cpp
Now my source code has been replaced by the executable program.Is there any way to retrieve my source-code. I'm new to Linux so I'm not sure if there is any way to undo what I've done.
Solution 1:
Probably not, try source control?
You might be lucky enough to have an editor open or a terminal window with scrollback.
And in the locking-the-barn-door-after-the-horse-has-bolted department, a good development practice even when working on toy programs is to use source code control.
Using either git or hg, you can do
$ hg init
$ hg add source.cpp
$ hg commit -m 'change' source.cpp
$ # edit here, and you can optionally revert to the original
$ hg commit -m 'change' source.cpp
$ # now if you clobber it you can go back to one of the previous revisions
Solution 2:
The option -o
specifies the output file, that is why the source code was overwritten.
You should have used
c++ source-code.cpp -o executable-name
As for retrieving the original source from the compiled file: no you cannot. You could disassemble it (so get an assembly version of your program) and I'm sure there is some little program out there that will rewrite some "C++ style" code from it, but that will never be like your original code as more than one instruction in C++ may correspond to the same machine code.