0

By mistake, I erased contents of my Fortran source file with a command involving ">":

some command > file.f

I do not use version control or anything. However, there is an object file present, file.o, if that may be of any help. Is there a chance to restore the contents of file.f?

2 Answers2

0

There may be decompiler tools that can produce Fortran source code from compiled object code, but it's not the original source code: things like comments and local variable names are discarded during the compilation process and are not present in the object file, so they can't be recovered. The structure of the decompiled code is likely to be different as well, especially if the object file was compiled with optimization.

You're not going to get your original code back from an object file, unfortunately.

Wyzard
  • 33,849
  • 3
  • 67
  • 87
0

Decompilation will work fine with bytecode languages like Java which are more or less "designed for that purpose".
With an optimizing compiler, such as Fortran (or C, or C++) you are pretty much out of luck.

Tools exist that restore some kind of source file (such as "boomerang"), but it will be nowhere near the original, and usually it is a waste of time even trying.

Given the nature of the compilation process, it is often not even possible to reverse the operation. Not only is information such as variable names or the names of non-exported functions (and of course comments) discarded and constants are replaced with magic numbers, but also the compiled program may have an entirely different structure than the code that you have written.
Compilers regularly perform optimizations like moving invariants out of loops, rearranging statements, or eleminating common subexpressions (even when optimizations are not explicitly enabled, most compilers do trivial optimizations anyway).

A compiler is required to produce something that behaves "as if" as observed from the outside, but not something that is necessarily equivalent to the source code that you have written.
A similar phenomenon exists when stepping through a program in a debugger. Sometimes, variables cannot be watched, or you cannot break on a particular line, and entire statements will apparently just be "gone" much to the surprise of the unaware developer because the compiler optimized them out.

In summary, the single best advice that I can give, unhelpful as it may be, is to acknowledge that you have done something stupid, rewrite the source file from scratch, and start using a version control system now.

Damon
  • 67,688
  • 20
  • 135
  • 185
  • Well, I'm using it, but for other projects. Guess I'll have to use it for all kinds of projects with no exceptions from now on. Thanks for the answer. – Alexander Lozovskiy Mar 19 '15 at 05:50