8

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.

Pavitar
  • 461

9 Answers9

15

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
DigitalRoss
  • 3,208
7

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.

nico
  • 175
6

Which editor did you use. Most probably there might be source-code.cpp~ backup file if you are using Vim or something.

2

oops ... you are out of luck.

First of all: Immediately unmount the file system the deleted file was located on. This minimizes the risk that the data of the deleted file are overwritten while taking steps to recover them. All data written to the file system containing the deleted file - either by you or by any other process running on your machine - might overwrite some of the data you want to recover!

more here: http://e2undel.sourceforge.net/recovery-howto.html

2

Try some disk recovering tool maybe the new file wasn't written on the same blocks as the original one. I am just thinking out loud here, but its better that you gave this a shot. Btw which editor are you using? Have you checked if your editor creates an automatic backup of every file?

Aaron S
  • 165
1

In the highly unlikely event that you haven't closed your vi session yet, open a new terminal and look for a .source-code.cpp.swp file in the same folder. Remember that files starting with a period are not listed by ls command by default; use ls -A to see them.

The last line of the .swp file would have the text from your original file before being corrupted by mal-compilation.

My original file test.cpp

#include <iostream>
using namespace std;

int main()
{
        cout << "Hello World!" << endl;
        cout << "Let us C";
}

The last part of my .test.cpp.swp (in this case, it contained a single huge line).

@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
@^@^@^cout << "Let us C";^@   cout << "Hello World!" << endl;^@{^@int main()^@^@using namespace std;^@#i    nclude <iostream>^@

The .swp file get deleted once you close the vi session; so if you've already closed it, you're out of luck.

Amarghosh
  • 703
0

no, unless you can undo your action through Linux, your source code is lost. You probably should have compiled it to a different name.

You cannot reverse-compile your source code.

0

I won't say it's impossible. The FBI can probably get some portion of it back with a huge heaping helping of luck. But since your executable is certainly larger than your source, you've overwritten all of it.

This is one reason why you should use a version control system. Check out SVN.

0

As @jase21 mentioned, if you'd used vim or gedit, there'll be a source.cpp~ file which contains a backup.

emacs will have a #source.cpp# file.

What text editor did you use? On a lighter note, how many lines of code lost?

slhck
  • 235,242
Karthick
  • 185