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.

9
  • 22
    Oh Myyy.. This is a great example of why we use source control.
    – JoshD
    Oct 4, 2010 at 5:07
  • 2
    Voting to move to super-user, since this is OT for SO. I do feel for ya Pavitar.
    – Ben Voigt
    Oct 4, 2010 at 5:10
  • 1
    @JoshD: Source control may not have helped in the situation where you check out, make a hundred changes, then compile to test before checking in (rule number 1: never break the build). What I long for are the days of VMS, where every file was versioned whenever it changed, and you could just back up one version to recover the source.
    – paxdiablo
    Oct 4, 2010 at 5:12
  • @paxdiablo: very true. In that case, use a text editor that automatically makes backup in .bak files on each save :) Journaling file systems seem to be coming into style, which kinda do what you've said. I think apple computers have just that capability.
    – JoshD
    Oct 4, 2010 at 5:14
  • 4
    @paxdiablo: That is why you have development branches, which you merge back once your changes got stable. (Ahem. That's also why you have Makefiles with tested-good compiler statements...)
    – DevSolar
    Oct 4, 2010 at 5:17

9 Answers 9

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
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.

2
  • You can just use: "g++ filename.cpp" and the run with "./a.out". You can disassemble using IDAPro (or some other powerful disassembler) from which you can recover the code.
    – jase21
    Oct 4, 2010 at 6:07
  • @jase21: sure, but probably a.out is not the best filename ever :). IDAPro will not recover the ORIGINAL code anyway.
    – nico
    Oct 4, 2010 at 6:10
6

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

4
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?

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.

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.

2
  • Decompilation is possible, but it won't return the original source code.
    – Ben Voigt
    Oct 4, 2010 at 5:12
  • It will likely make little sense.
    – Alexander Rafferty
    Oct 4, 2010 at 5:22
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
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?

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .